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

Discussion Overview

The discussion revolves around a Java program that is intended to read from an input file and write to an output file, but the output file is not being created as expected. Participants explore potential reasons for this issue, including file paths and program execution context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports that the program does not create an output file and seeks help in identifying the issue.
  • Another participant suggests that the problem may relate to the working directory when the program is run, proposing that the output file might be created in a different location.
  • A later reply indicates that the output file may not be visible while the program is still running in a debugger, recommending running the code as a standalone program from the command line.
  • Another suggestion is made to explicitly flush the buffer and dispose of the file object to ensure that Java releases its grip on the file, as I/O operations can be complex.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as multiple competing views and suggestions are presented regarding the cause of the issue and potential solutions.

Contextual Notes

There are limitations regarding the assumptions about the working directory and file visibility during program execution, which remain unresolved.

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