5

I'm having trouble with writing to a txt file. I am getting a FileNotFound Exception, but I don't know why because the file most definitely is there. Here is the code.

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.File;

public class Save
{
    public static void main(String[] args)
    {
        File file = new File("C:/Users/Daniel/Desktop/Programs/Save Data Test/save.txt");
        PrintWriter pw = new PrintWriter(file);
        pw.println("Hello World");
        pw.close();
    }
}
user3720913
  • 105
  • 1
  • 8
  • 4
    Your code doesnt compile - PrintWriter throws that `FileNotFoundException` which needs to be handled – Reimeus Apr 18 '15 at 18:09
  • 1
    Have a look at this: http://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java – LarsBauer Apr 18 '15 at 18:09
  • 1
    The code above wouldn't even compile. So there's no way you can get an exception. – JB Nizet Apr 18 '15 at 18:12
  • 1
    Your title is literally just tags. Please update it to be a brief, meaningful description of your problem. See [How to Ask](http://stackoverflow.com/help/how-to-ask) for more information. – Nic Apr 18 '15 at 19:02

3 Answers3

9

You must create the actual file with its directory before you create the PrintWriter put

file.mkdirs();
file.createNewFile();

Using this with the proper try and catch blocks would look something like this...

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

public class Save
{
    public static void main(String[] args)
    {
        File file = new File("save.txt");
        try {
            file.mkdirs();
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            PrintWriter pw = new PrintWriter(file);
            pw.println("Hello World");
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}
Daniel K.
  • 1,326
  • 2
  • 12
  • 18
  • 2
    The asker said that "the file most definitely is there", which means that this won't solve it. – Nic Apr 18 '15 at 19:03
  • 1
    False. According to the Java documentation, the File parameter is "The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered." [Java 8 PrintWriter](https://docs.oracle.com/javase/8/docs/api/) – Andrew Kirna May 05 '18 at 17:52
2

Just because you know the file is there, doesn't mean your code should not check for its existence before attempting to process.

As far as your FileNotFound Exception, some if not all Java IDEs force you to write try/catch blocks if the IDE detects that an exception can occur.

NetBeans for example, The code won't even compile:

enter image description here

You have to code a try/catch block to handle a potential exception

public static void main(String[] args) {
    File file = new File("C:/Users/Daniel/Desktop/Programs/Save Data Test/save.txt");
    if (file.exists()) {
        try {
            PrintWriter pw = new PrintWriter(file);
            pw.println("Hello World");
            pw.close();
        } catch (FileNotFoundException fnfe){
            System.out.println(fnfe);
        }
    }
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

turn this: public static void main(String[] args)

into this:

public static void main(String[] args) throws FileNotFoundException

and it will work just fine.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36