2

I'm sure I'm missing something basic here. I'm trying to create a new file on my drive, but I'm getting an error:

Exception in thread "main" java.io.FileNotFoundException: C:\ProgramData\msena\test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at net.meosoft.relatetoit.core.HibernateSessionFactory.main(HibernateSessionFactory.java:89)

My code at the moment is:

    final File file = new File("C:\\ProgramData\\uname2\\test.txt");
    final BufferedReader in = new BufferedReader(new FileReader(file));
    while(in.ready()) {
        System.out.println(in.readLine());
    }
    in.close();

What's wrong at the moment? I want to just read, even if it's there (so file should be made).

halphhurts
  • 73
  • 1
  • 1
  • 9
  • I'd like to have the file made when I create it though.. – halphhurts Aug 14 '12 at 01:06
  • 3
    The issue is pretty obvious. The `C:\ProgramData\msena\test.txt` does not exist in this location, so you can't open to read it. Check if the file is actually there or make sure you have permission to read the file. – CoolBeans Aug 14 '12 at 01:07
  • 3
    But you're not creating it. You're just reading it. – user207421 Aug 14 '12 at 01:08
  • Why would you want to read it if it didn't exist? – km1 Aug 14 '12 at 01:18
  • Possible duplicate of [How to create a file in a directory in java?](https://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java) – Bernhard Barker Mar 25 '18 at 08:45

4 Answers4

9

Java doesn't automatically check that File() exists, nor will it automatically create it if you ask it.

You'll need to do one of the following:

  1. Add in a check for the file's existence: if(file.exists()) { ... }.
  2. Add in a check, similar to above, but then if it doesn't exist, call: file.createNewFile();. This will make a new file on the file system for you to use.

If that still doesn't work, I'd check you have write permissions to that directory. :)

Michael
  • 1,014
  • 1
  • 8
  • 23
  • That looks right.... sigh. I wish it could create on initialize. I missed that... – halphhurts Aug 14 '12 at 01:12
  • No worries; just flag the question when you're done. Welcome to stack overflow :P – Michael Aug 14 '12 at 01:13
  • I personally thought that just calling the constructor new File will actually create a file, but I was surprised when I called `file.exists()` and it returned `false`, I am not sure why new File does not create a file. – Bionix1441 Sep 13 '17 at 08:08
2

The File class represents the path to a file, not the file itself. If the file does not exist (!File.exists()), an exception will be thrown when you try to access it. Make sure the path to the file is correct and that you have permission to read from that location.

If you want to create the file, you can use File.createNewFile().

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
0

this is the method to create file.

Formatter output;//pointer to an object that will write to a file
public void createFile(){
try{
  output = new Formatter("C:\\ProgramData\\uname2\\test.txt");
  //test.txt is the name of the file to be created
  //create file in the same folder called test.txt
  //if existed overwrite it
    }
  catch(FileNotFoundException e){
  System.err.println("Error creating file"+e.getMessage());
 }

call createFile() in the main

 CreateTextFile file = new CreateTextFile();
 file.createFile();
Osama Ahmad
  • 235
  • 3
  • 5
  • 14
0

Check your file name. It should not contain any colon.

Fazal
  • 3,374
  • 1
  • 15
  • 20