Java Cant get the outputfile in Java program

  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Java Program
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
3
Views
3K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
3
Views
3K
Back
Top