2) A common IT task is to take a data file and process it so that it can be uplo
ID: 669248 • Letter: 2
Question
2) A common IT task is to take a data file and process it so that it can be uploaded to a database. You are given a file in the following format:
Fred:1211 My Street:Houston:Texas
Ethel:1212 Another Street:Snook:Texas
... 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:
Fred|1211 My Street|Houston,Texas
Ethel|1212 Another Street|Snook,Texas
… Notice that in the input file, the city and state are two separate entries while in the output, they are combined using a comma.
The function convertFile does not require any parameters. You must call convertFile from the main function. The input file should be called “input_data.txt”. The output should be called “_hm1_output.txt”, where is your username
Explanation / Answer
working java code compiled on ideone
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
File dir = new File(".");
String source = dir.getCanonicalPath() + File.separator + "input_data.txt";
String dest = dir.getCanonicalPath() + File.separator + "_hm1_output.txt";
File fin = new File(source);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
FileWriter fstream = new FileWriter(dest, true);
BufferedWriter out = new BufferedWriter(fstream);
String aLine = null;
while ((aLine = in.readLine()) != null) {
aLine.replace(':',',');
out.write(aLine);
out.newLine();
}
in.close();
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.