can anyone help me with this Java Problem. it would be helpfull if the code is n
ID: 669315 • Letter: C
Question
can anyone help me with this Java Problem. it would be helpfull if the code is not complicated or advance at all. im a beginner in java and I really want to understand the code. if anyone can help thanks a lot.
In this exercise, you are supposed to create a program.
Inside the program, there should be an instanced method called convertFile().
This method should is supposed to read a file called “input_data.txt”, modify the data that has been read (without changing “input_data.txt”) and store it in a file called “<name>_hm1_output.txt”, where <name> is your username. In my case, that file should be "username_hm1_output.txt”.
Explanation / Answer
package mani;
import java.io.*;
import java.util.Scanner;
public class temp{
public void convertFile(){
String fileName = "C:\Users\MANIKANTA Reddy\workspace\chegg\src\mani\input_data.txt";
String line = null;
String username;
Scanner s=new Scanner(System.in);
System.out.println("Enter the username: ");
username=s.next();
username=username+"_hm1_output.txt";
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
File fout = new File(username);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while((line = bufferedReader.readLine()) != null) {
System.out.print(line);
bw.write(line);
bw.newLine();
}
bw.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
public static void main(String [] args) {
temp t=new temp();
t.convertFile();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.