Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cant get the outputfile in Java program

  1. Oct 6, 2013 #1
    Im using Eclipse and I have an input file name that I am reading from but for some reason when the program ends there is no output file that gets created? Does anyone know what I am doing wrong here?

    Code (Text):

     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.PrintWriter;
     import java.util.Scanner;
     
     public class NameAgeReverse{

        public static void main(String[] args) throws FileNotFoundException
        {
           
            Scanner enter = new Scanner(System.in);
            String inputFile, outputFile;
            File input;
            PrintWriter output;
           
            System.out.println("Input file: ");
            inputFile= enter.nextLine();
            System.out.println("Output file: ");
            outputFile= enter.nextLine();
           
            input = new File(inputFile);
            Scanner in = new  Scanner(input);
            output = new PrintWriter(outputFile);
           
            while(in.hasNextLine()){
               
                String name = in.nextLine();
                output.println(name);
               
            }
                   
            in.close();
            output.close();
                   
        }  
    }
     
  2. jcsd
  3. Oct 6, 2013 #2
    I see nothing wrong that would stop it from working. Plus, a quick test shows it creating the output file, for me. I think it's a matter of what the working directory is when you run it. That's where the file should show up. There's probably an output file created somewhere. You could try using an output filename containing the entire full path to see if it shows up there.
     
  4. Oct 6, 2013 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    What grep said, plus, you may not be able to "see" the output file while the program is still active in a debugger, because your OS might not finish updating the file system on your hard disk until the application terminates completely. Try running the code as a stand alone program from the command line, not in your debugger.

    Re the file paths, you must be finding the input file, otherwise you would throw an exception when you try to open it. So you can make a good guess at where the output file "should" be created.
     
  5. Oct 8, 2013 #4

    harborsparrow

    User Avatar
    Gold Member

    You might need to explicitly flush the buffer and then dispose of the file object to make Java "release" its grip on the file so that you can see if on the file system. I/O tends to be a little arcane in any language.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Cant get the outputfile in Java program
  1. Java programs (Replies: 9)

  2. Java Programming (Replies: 12)

Loading...