i need to make this program write to a file, turning all letters to upper case.
ID: 3830902 • Letter: I
Question
i need to make this program write to a file, turning all letters to upper case. I m able to read in the file and read out but at the moment there is nothhing showing up on the file im writing to. i also dont know how to write the code to make all upper letters. There needs to be a try with resource that is why i have the resources insside the try block.
public class ConvertToUpperCase {
public static void main(String[] args) {
try {
BufferedReader in =
new BufferedReader(new FileReader(new File("src/finalPrep/fileIo/Quote.txt")));
System.out.println("File open successful!");
//some code to make the file all upper letters
PrintWriter writer =
new PrintWriter(new File("src/finalPrep/fileIo/upper.txt"));
System.out.println("file was written on");
writer.println(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Explanation / Answer
Hi
I have modified the code and highlighted the code changes below.
ConvertToUpperCase.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ConvertToUpperCase {
public static void main(String[] args) throws IOException {
try {
BufferedReader in =
new BufferedReader(new FileReader(new File("src/finalPrep/fileIo/Quote.txt")));
System.out.println("File open successful!");
//some code to make the file all upper letters
String s ="";
String line ="";
while((line=in.readLine()) !=null)
{
s = s + line.toUpperCase()+" ";
}
PrintWriter writer =
new PrintWriter(new File("src/finalPrep/fileIo/upper.txt"));
System.out.println("file was written on");
writer.println(s);
writer.flush();
writer.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.