reading a text file , uppercase File converter , Writing to a file create yourse
ID: 3554030 • Letter: R
Question
reading a text file , uppercase File converter , Writing to a file create yourself a text file that has at least four lines of text. Write a program that asks the user for the names of two files. The first file should be opened for reading and the second file should be opened for writing. Your program should read the contents of the text file , change all the characters to uppercase, and store the results in the second file.
To Reiterate, your program should:
1 Prompt for the name of the text file( Scanner or JOptionPane Class),
2. open the text file that you will read from.
3.prompt for the name of the file you will write to,
4.open the text file that you will write to,
5 read the contents of your text file,
6. change all the characters to uppercase,
7. and store the results in the second file,
8. close both the text files.
The second file ( the one you write to) will be a copy of the first file ( the one you read from), except that all the characters will be uppercase. Use can use either the scanner or JOptionPane class to read in your file names. Be sure to include your text file.
Explanation / Answer
package lower_upper;
import java.io.*;
import java.util.Scanner;
public class lower_upper{
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the text file with the extension to read from: ");
String name = in.nextLine();
BufferedReader br = new BufferedReader(new FileReader(name));
System.out.println("Enter the name of the text file with the extension to write to : ");
String name2=in.nextLine();
//BufferedReader br1 = new BufferedReader(new FileReader(name2));
String line = br.readLine();
FileWriter fstream = new FileWriter(name2);
BufferedWriter out = new BufferedWriter(fstream);
while (line != null) {
//StringBuilder sb = new StringBuilder();
//System.out.println(line);
String upper = line.toUpperCase();
//sb.append(upper);
//sb.append(" ");
out.write(upper);
out.newLine();
//out.write(" ");
//System.out.println(upper);
line = br.readLine();
}
//Close the output stream
out.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.