Write a Java program which reads a text file and writes the content into a new f
ID: 3777408 • Letter: W
Question
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.
Be sure to use comments in your program
You will need to submit both the Java file and the text file during submission.
Explanation / Answer
datafile999.txt( Save this file under D Drive.Then the path of the file pointing to it is D:\datafile999.txt)
A computer is a device that can be instructed to carry out
an arbitrary set of arithmetic or logical operations
automatically. The ability of computers to follow a
sequence of operations, called a program make computers
very flexible and useful Such computers are used as
control systems for a very wide variety of industrial
and consumer devices
________________
UppercaseToLowerCase.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class UppercaseToLowerCase {
public static void main(String[] args) {
//Storing the name of the file in the String
String filename1="D:\datafile999.txt";
//Storing Output file name to a String
String filename2="D:\datafile888.txt";
String line,line1="";
char ch;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(filename1);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
//Creating FileWriter object by passing output file name as input.
FileWriter fw=new FileWriter(filename2);
BufferedWriter bw=new BufferedWriter(fw,1024);
/*
* Read each line as String from the text file
* And convert it into lower case letter if it is upper case letter.
*/
while ((line = bufferedReader.readLine()) != null) {
for(int i=0;i<line.length();i++)
{
ch=line.charAt(i);
if(ch>=65 && ch<=90)
{
//Converting upper case character to lower case character
ch+=32;
//Writing to a file
bw.write(ch);
}
else
//Writing character to a file
bw.write(ch);
}
bw.newLine();
}
// Always close files.
bufferedReader.close();
bw.close();
fw.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + filename1 + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '"+ filename1 + "'");
}
}
}
_____________________________
datafile888.txt (We can find this file under D Drive.as we specified the path of the output as D:\datafile888.txt)
a computer is a device that can be instructed to carry out
an arbitrary set of arithmetic or logical operations
automatically. the ability of computers to follow a
sequence of operations, called a program make computers
very flexible and useful such computers are used as
control systems for a very wide variety of industrial
and consumer devices
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.