Cant get the outputfile in Java program

  • Context: Java 
  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary
SUMMARY

The discussion centers on a Java program using Eclipse that fails to create an output file after execution. The code provided reads an input file and writes its contents to an output file using the PrintWriter class. Key issues identified include the working directory of the program and potential delays in file system updates by the operating system. Recommendations include specifying the full path for the output file and running the program outside of the debugger to ensure proper file creation and visibility.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of file I/O in Java
  • Familiarity with Eclipse IDE
  • Basic knowledge of command line operations
NEXT STEPS
  • Learn about Java PrintWriter and its buffering behavior
  • Research file path handling in Java, including absolute vs. relative paths
  • Explore debugging techniques in Eclipse for file I/O operations
  • Investigate how operating systems manage file system updates during program execution
USEFUL FOR

Java developers, software engineers, and anyone troubleshooting file I/O issues in Java applications.

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.
 

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