Question & Answer: Write a program that reads each line in a file, reverses its characters, and writes the resulting line to the same file……

Write a program that reads each line in a file, reverses its characters, and writes the resulting line to the same file. Use the following pseudocode: While the end of the file has not been reached pos1 = current get position Read a line If the line was successfully read pos2 = current get position Set put position to pos1 Write the reversed line Set get position to pos2

Expert Answer

 import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CharReverse{
public static void main(String args[]) throws FileNotFoundException, IOException{
BufferedReader br=new BufferedReader(new FileReader(“C:\Users\SuNiL\Desktop\HW3Output.txt”));
String str,rev=””;
char st[] = null;
int length=0;
try {
while((str=br.readLine())!=null){
System.out.println(str);
length=str.length();
st=new char[length];
for(int i=length-1;i>=0;i–){
rev=rev+str.charAt(i);
st[length-i-1]=str.charAt(i);
}
rev=rev+”n”;
}
PrintWriter pw=new PrintWriter(new FileWriter(“C:\Users\SuNiL\Desktop\HW3Output.txt”));
pw.print(rev);
pw.flush();

}catch (IOException ex) {
Logger.getLogger(CharReverse.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

Still stressed from student homework?
Get quality assistance from academic writers!