can anyone help me with this Java exercise. it would be better if the code is no
ID: 669493 • Letter: C
Question
can anyone help me with this Java exercise. it would be better if the code is not complex, just basic stuff. Im starting java and I really want to understand it.
Create a class called FileConverter. In the class, create an instance method called convertFile() that reads in the datafile and writes it to a new file in the following format:
james:1165 Street: miami:fl
dane:1234 Another Street:miami:fl so this info is in a textfile called input.txt
so it reads this info from the text file called input.txt and writes it in a new text file named hm_new_output.txt in this new format
james|1165 Street| miami,fl
dane|1234 Another Street|miami,fl
Explanation / Answer
import java.io.*;
public class FileConverter {
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
BufferedReader br = new BufferedReader(new FileReader(file))
String line;
while ((line = br.readLine()) != null) {
int lastIndex = line.lastIndexOf(':');
line = line.substring(0,lastIndex - 1) + "," + line.substring(lastIndex + 1);
line = line.replace(':','|');
out.write(line + " ");
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.