Program#2: Work programming exercise 17.4, page 701, Convert a text file into UT
ID: 3914961 • Letter: P
Question
Program#2: Work programming exercise 17.4, page 701, Convert a text file into UTF. Follow the specifications given in the problem statement. You don't have to run the program form the command line as the problem statement asks for, just run it from the IDE. 17.4 (Convert a text file into UTF Write a program that reads lines of characters from a text file and writes each line as a UTF-8 string into a binary file. Display the sizes of the text file and the binary file. Use the following command to run the program: java Exercise17 04 Welcome.java Welcome.utf Please write this Java program as simple as possible and with comments throughout explaining what is happening. Thank youExplanation / Answer
import java.io.*;
public class UTF {
public static void main(String args[]){
try{
//take input file
FileInputStream fis = new FileInputStream("input.in");
//attaching file to inputstream reader
InputStreamReader isr = new InputStreamReader(fis, "ISO-8859-1");
//reader for reading the content
Reader in = new BufferedReader(isr);
//file output stream for writing purpose
FileOutputStream fos = new FileOutputStream("test.out");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Writer out = new BufferedWriter(osw);
//read the content character by character
int ch;
while ((ch = in.read()) > -1) {
out.write(ch);
}
System.out.println("succesfully converted text file to UTF");
out.close();
in.close();
}catch(Exception e){
System.out.println("may be input file missing check once");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.