Cant get the outputfile in Java program

In summary, the code seems to be trying to open the input file, but it can't find it, so it tries to look for the output file in the same place.
  • #1
camel-man
76
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
  • #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.
 
  • #3
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.
 
  • #4
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.
 
  • #5


It is difficult to determine the exact issue without seeing the specific code you are using, but there are a few common mistakes that could be causing your problem.

First, make sure that you are actually creating and writing to the output file. In your code, you have created a PrintWriter object for the output file, but you have not actually written anything to it. You need to use the PrintWriter's "println" or "print" methods to write to the file.

Second, check that you are providing the correct file path for your output file. If you are using relative paths, make sure that the output file is being created in the correct location. If you are using an absolute path, double check that the file is being created in the specified location.

Lastly, make sure that you are closing the output file properly. In your code, you are closing the PrintWriter, but you are not closing the Scanner for the input file. This could potentially cause issues with the output file not being created.

I would also recommend using try-catch blocks to handle any potential exceptions that may occur while writing to the file, as this can help identify and resolve any issues with file creation.

Overall, it is important to carefully review your code and make sure that all necessary steps are being taken to create and write to the output file. If you continue to have issues, you may want to seek assistance from a more experienced programmer or consult online resources for troubleshooting help.
 

1. Why is my Java program not producing an output file?

There could be several reasons for this. Some common causes include incorrect file path, errors in the code, or the program not being executed properly. Double check your file path and make sure it is correct. Also, check for any errors in your code and make sure the program is being executed correctly.

2. How can I specify the output file path in my Java program?

You can use the File class to specify the output file path in your Java program. First, create a File object with the desired file path. Then, use the FileWriter class to write to the file. Make sure to handle any exceptions that may arise.

3. How can I debug my Java program if it is not producing an output file?

You can use a debugger to step through your code and check for any errors or bugs. You can also use System.out.println() statements to print out the values of variables at different points in your code to help identify any issues.

4. Can I use relative file paths for my output file in a Java program?

Yes, you can use relative file paths for your output file in a Java program. However, make sure that the relative path is correct and that the file is being written to the correct location.

5. How can I check if my Java program successfully created an output file?

You can use the File.exists() method to check if the output file was successfully created. This method will return a boolean value of true if the file exists and false if it does not.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
3
Views
778
  • Programming and Computer Science
Replies
2
Views
641
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top