Using JAVA, read the following text file and write a new text file in the format
ID: 3805978 • Letter: U
Question
Using JAVA, read the following text file and write a new text file in the format shown below:
inputFile.txt
0
25 244
108 275
140 273
159 313
219 199
254 392
369 171
518 271
538 250
568 253
603 307
613 196
638 314
1
24 187
43 182
65 331
155 369
outputFile.txt
0,25,244
0,108,275
0,140,273
0,159,313
0,219,199
0,254,392
0,369,171
0,518,271
0,538,250
0,568,253
0,603,307
0,613,196
0,638,314
1,24,187
1,43,182
1,65,331
1,155,369
Explanation / Answer
ReadWriteFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReadWriteFile {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("D:\inputFile.txt");
File outputFile = new File("D:\outputFile.txt");
PrintWriter pw = new PrintWriter(outputFile);
String s = "", str="";
if(inputFile.exists()){
Scanner inputScan = new Scanner(inputFile);
while(inputScan.hasNextLine()){
String line = inputScan.nextLine();
if(!line.trim().equals("0") && !line.trim().equals("1") ){
str =str+ s + ","+line.split("\s+")[0]+","+line.split("\s+")[1]+" ";
pw.write(str);
System.out.println(str);
str="";
}
else{
s = line;
}
}
pw.flush();
pw.close();
inputScan.close();
}
else{
System.out.println("inputFile.txt file does not exist");
}
}
}
Output:
0,25,244
0,108,275
0,140,273
0,159,313
0,219,199
0,254,392
0,369,171
0,518,271
0,538,250
0,568,253
0,603,307
0,613,196
0,638,314
1,24,187
1,43,182
1,65,331
1,155,369
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.