Write a Java program which reads a text file and writes the
Write a Java program which reads a text file and writes the content into a new file. During the read-write process, convert all the upper case letters into lower case ones. In other words, your programming task is to create a new file with the same content of the original file, only that all the upper case letters are converted into lower case ones in the new file.
Create your own text file to test your program. You will need to submit both the Java file and the text file during submission.
Solution
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class UpperLower { // class to run the code
public static void main(String[] args) throws IOException { // driver method
File inputFile = new File(\"input.txt\"); // file objects for the respective text files
File outputFile = new File(\"out.txt\");
BufferedReader in = (new BufferedReader(new FileReader(inputFile))); // reader and the writer class objects
PrintWriter out = (new PrintWriter(new FileWriter(outputFile)));
try {
System.out.println(\"Reading Data From the Input File...\"); // message
String sCurrentLine; // local variables
System.out.println(\"Converting the UpperCase Letters to LowerCase...\"); // message
while ((sCurrentLine = in.readLine()) != null) { // read the data line by line
sCurrentLine = sCurrentLine.toLowerCase(); // conver the data to lowercase
out.print(sCurrentLine + \"\ \"); // write the data
}
System.out.println(\"Successfully Written Data to the New File..!\"); // message
} catch (IOException e) { // catch the exceptions any
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close(); // close the input file
}
if (out != null) {
out.close(); // close the output file
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
OUPUT :
Reading Data From the Input File...
Converting the UpperCase Letters to LowerCase...
Successfully Written Data to the New File..!
input.txt :
This is John
I had worked in WiPro and Cisco.
I have an ExpErience oF 5 YearS in the StreaM of CompuTerS and TechNolOGy.
out.txt :
this is john
i had worked in wipro and cisco.
i have an experience of 5 years in the stream of computers and technology.
Hope this is helpful.

