Assignment:
Uppercase File Converter
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. The program should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, except that all the characters will be uppercase. Use Notepad or another text editor to create a simple file that can be used to test the program.
Notepad:
Frankie owned a ferocious feline named Freddy as a pet. Freddy was funny and furry. His fur is bright red with black stripes.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package uppercasefileconverter; import java.util.Scanner; import java.io.*; /** * * @author uuuuuuu */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { Scanner keyboard = new Scanner(System.in); String filename1; String filename2; System.out.println("Enter name of file 1:"); filename1 = keyboard.nextLine(); System.out.println("Enter name of file 2:"); filename2 = keyboard.nextLine(); File file1 = new File(filename1); Scanner inputFile = new Scanner(file1); inputFile.close(); PrintWriter outputFile = new PrintWriter(filename2); outputFile.println(filename1.toUpperCase()); outputFile.close(); } }
The file names that I will be using are:
testfile.txt
testfileCopy.txt
What do I need to fix in the code?
Error pops up because of throws IOException.
run: Enter name of file 1: testfile.txt Enter name of file 2: testfileCopy.txt Exception in thread "main" java.io.FileNotFoundException: testfile.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.util.Scanner.<init>(Scanner.java:636) at uppercasefileconverter.Main.main(Main.java:32) Java Result: 1 BUILD SUCCESSFUL (total time: 1 minute 40 seconds)