The program suppose to open a file when the user enters 1, then it should perfor
ID: 3625027 • Letter: T
Question
The program suppose to open a file when the user enters 1, then it shouldperform any of the operations listed on the menu. My program performs the first 5 tasks, except Case 6: ReverseByLine, and Case 7: ReverseTheWhole document. We are working with text files only.
Can anybody check my code and help me to figure out what is wrong with my program.
PS. I will rate life saver
import java.util.Scanner;
import java.io.*;
public class p2 {
public static void main(String[] args)throws FileNotFoundException, IOException {
System.out.println("ATTENTION: 1-MUST BE YOUR FIRST CHOICE! ");
int option;
File file = new File("");
Scanner in = new Scanner(System.in);
option = menu(in);
while(option !=1 && option !=0) {
option = menu(in);
}
do {
switch(option)
{
case 0: System.exit(0); //exits the program
break;
case 1: file = select(file, in); //selects a file
break;
case 2: display(file); //displays the file selected
break;
case 3: file = newName(file, in); //renames the file
break;
case 4: upper(file); //change file content to Upper case
break;
case 5: lower(file);//Change file content to lower case
break;
case 6: reverseByLine(file);
break;
case 7: reverseWholeData(file, in);//reverses the whole data in file
break;
}
option = menu(in);
}while(option !=0);
}
/*The menu method displays the options to select on the screen*/
public static int menu(Scanner in) {
int n;
do {
System.out.println(" MENU");
System.out.println("0 - Exit ");
System.out.println("1 - Select file ");
System.out.println("2 - Display ");
System.out.println("3 - Rename ");
System.out.println("4 - To upper case ");
System.out.println("5 - To lower case ");
System.out.println("6 - Reverse each line ");
System.out.println("7 - Reverse the whole file ");
System.out.println("What do you want to do with the file? ");
n=in.nextInt();
if(n<0||n>7)
System.out.println("Invalid entry");
}while(n<0||n>7);
return n;
}
/*This method prompts the user to select a file*/
public static File select(File f,Scanner in) {
String fileName;
System.out.print("Enter a filename(animal.txt or place.txt): ");
fileName=in.next();
f=new File(fileName);
System.out.print("File selected: "+ fileName + " ");
return f;
}
/*This method displays the file selected on the screen*/
public static void display(File f)throws FileNotFoundException {
try {
FileInputStream fStream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fStream); //Get the object of DataInputStream
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String buffer = null;
//Read File line by line
while ((buffer = br.readLine())!= null) {
System.out.println(buffer);//Print the content on the console
}
in.close();//Close the input stream
}catch (Exception e){//catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
/*This method is used to rename the File selected*/
public static File newName(File f, Scanner in) {
System.out.print("Enter the file to be renamed: ");
String old_name = in.next();
File oldFile=new File(old_name);
if(!oldFile.exists()){
System.out.println("File does not exists: ");
System.exit(0);
}
System.out.print("Enter the name for the new file : ");
String new_name = in.next();
File newFile = new File(new_name);
System.out.println("Name of the old file : "+ oldFile);
System.out.println("New File name : "+ new_name);
boolean Rename = oldFile.renameTo(newFile);
if(!Rename)
{
System.out.println("File or directory does not rename successfully.");
System.exit(0);
}
else {
System.out.println("File or directory rename is successfully.");
}
return f;
}
/*This method change the file selected to UPPER Case*/
public static void upper(File f)throws FileNotFoundException,IOException {
FileInputStream in = new FileInputStream( f );
int len = (int) f.length();
byte buf[] = new byte[len];
char cbuf ;
int offset = 0;
int rc = 0;
while( offset < len ) {
rc = in.read( buf, offset, len - offset );
offset += rc;
}
in.close();
for(int i=0;i<len;i++) {
cbuf=Character.toUpperCase((char)buf[i]);
buf[i]=(byte)cbuf;
}
rc = 0;
OutputStream oldOut = getNewOutputStream();
FileOutputStream newOut=new FileOutputStream(f);
oldOut.write(buf);
newOut.write(buf);
oldOut.close();
newOut.close();
}
public static OutputStream getNewOutputStream(){
OutputStream out = new ByteArrayOutputStream(2000);
return out;
}
/*This method changes the file selected to lower case
*throws FileNotFoundException and IO exceptions if occur */
public static void lower(File f)throws FileNotFoundException,IOException {
FileInputStream in = new FileInputStream( f );
int len = (int) f.length();
byte buf[] = new byte[len];
char cbuf ;
int offset = 0;
int rc = 0;
while( offset < len ) {
rc = in.read( buf, offset, len - offset );
offset += rc;
}
in.close();
for(int i=0;i<len;i++) {
cbuf=Character.toLowerCase((char)buf[i]);
buf[i]=(byte)cbuf;
}
rc = 0;
OutputStream oldOut = getNewOutputStream();
FileOutputStream newOut=new FileOutputStream(f);
oldOut.write(buf);
newOut.write(buf);
oldOut.close();
newOut.close();
}
/*This method reverses the file line by line*/
public static void reverseByLine(File f2) throws FileNotFoundException, IOException {
try {
FileInputStream fsi = new FileInputStream(f2);
DataInputStream in = new DataInputStream(fsi);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line[]=new String[6];
String rev[]=new String[6];
char ch;
int k=0;
while((line[k]=br.readLine())!=null)
{
int l=line[k].length();
char a[]=new char[1];
line[k].getChars(0,1,a,0);
for(int i=0, j=l-1;i<1/2;i++,j--){
ch=a[i];
a[i]=a[j];
a[j]=ch;
}
rev[k]=new String(a);
k++;
}
br.close();
System.out.println("Original Data in the file: ");
for(int i=0;i<6; i++)
System.out.println(line[i]);
PrintStream ps = new PrintStream(new FileOutputStream(f2));
for(int i=0;i<k;i++){
ps.println(line[i]);
}
ps.close();
System.out.println("After reversing data in the file: ");
for(int i=0;i<6;i++)
System.out.println(rev[1]);
}catch(FileNotFoundException fne){
System.out.println("Can not find the file");
}
}
/*This method reverses the whole file*/
public static void reverseWholeData(File f, Scanner in) throws IOException {
try {
String revFile = in.next();
int len=(int) (new File(revFile).length());
FileInputStream fis = new FileInputStream(revFile);
byte buf[] = new byte[len];
fis.read(buf);
fis.close();
System.out.println("Original Data in the file: ");
for(int i=0;i<len;i++)
System.out.print((char)buf[i]);
PrintWriter ps2 = new PrintWriter(new FileOutputStream("newp2.txt"));
for(int i = len-1;i>0;i--){
ps2.write((char)buf[i]);
}
ps2.close();
System.out.println("After reversing data in file: ");
for(int i=len-1;i>=0;i--)
System.out.print((char)buf[i]);
} catch(FileNotFoundException fne2) {
System.out.println("Can not find the file");
}
}
}
Explanation / Answer
See if this makes sense (if working in Windows, open files with Wordpad as Notepad does not like " " newline character): public static void reverseByLine(File f2) throws FileNotFoundException, IOException { Scanner myScanner = new Scanner(f2); LinkedList fileLinesList = new LinkedList(); String reversedFileString = ""; int numberOfLines = 0; while(myScanner.hasNextLine()) { fileLinesList.add(myScanner.nextLine()); } myScanner.close(); numberOfLines = fileLinesList.size(); for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.