Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Input/Output file help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input/Output file help

    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)
    Last edited by Plural; October 23rd, 2010 at 06:06 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Input/Output file help

    Quote Originally Posted by Plural View Post
    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)
    Your file doesn't exist apparently. You told it to throw that exception if there was no file of that name.


    I think I see the problem. You're having them create a File. That file is empty. You're scanning nothing.
    Last edited by javapenguin; October 23rd, 2010 at 06:21 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input/Output file help

    Quote Originally Posted by javapenguin View Post
    Your file doesn't exist apparently. You told it to throw that exception if there was no file of that name.
    I have it on my computer, but it somehow can't find it.
    Where should I be saving it?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Input/Output file help

    Also, you closed the input file. It won't give any more input to be scanned after the .close.

    You can certainly, I think, tell it to open a File by having the user give it a name, but if it doesn't match the name of your input file, it'll throw the FileNotFoundException. Also, you never read anything from it. You just close it. Or so it seems from your code. Then you ask it to output it to another File, and you output to that one merely the name that the user gave for fileOne in upper case.

    Also, you never put anything from that name. Unless the file already exists.

    However, it won't read anything unless you do something like:

    while (inputFile.hasNext())


    Then you would do something like this:

    String s = inputFile.nextLIne();

    Then later, you'd have to write it all back.
    Last edited by javapenguin; October 23rd, 2010 at 06:35 PM.

Similar Threads

  1. Output an int i to a file
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 10:45 AM
  2. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  3. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM
  4. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM