Java Cant get the outputfile in Java program

  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary
The Java program in question is intended to read from an input file and write to an output file, but users report that the output file is not being created. The issue may stem from the working directory where the program is executed, as the output file could be generated in a different location. It is suggested to specify the full path for the output filename to ensure it is saved correctly. Additionally, running the program outside of a debugger may help, as file system updates can be delayed while the application is active. Flushing the output buffer and properly closing the file may also be necessary to ensure the file is visible after execution.
camel-man
Messages
76
Reaction score
0
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:
 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();
				
	}	
}
 
Technology news on Phys.org
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.
 
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.
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
3
Views
3K