0

My code is very simple and straight forward. I create an empty file using the PrintWriter class and write the numbers 1 - 100 to the file, then close the file. As far as I know, PrintWriter should create the empty file, which makes me wonder why I'm getting this FileNotFoundException error.

public class Practice {

    public static void main(String[] args){

      PrintWriter outputFile = new PrintWriter("nums.txt");

      for(int i = 0; i < 100; i++)
          outputFile.println(i + 1);

      out.close();

    }
}
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
Raw_Data
  • 19
  • 2
  • The code you are showing could not have possibly thrown a `FileNotFoundException`. – Mike Nakis Aug 03 '17 at 15:28
  • Could you show the full error message? – beastlyCoder Aug 03 '17 at 15:29
  • 1
    Maybe you try to execute the code in a read-only directory. Can you check? – Arnaud Denoyelle Aug 03 '17 at 15:30
  • 1
    @MikeNakis yes it can, https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter(java.lang.String). Try changing the path and see. `"C:/Documents/nums.txt"` – Eddie Martinez Aug 03 '17 at 15:30
  • [Here's a link. This should help](https://stackoverflow.com/a/19309163/8210845) – user8210845 Aug 03 '17 at 15:34
  • https://stackoverflow.com/a/19309163/8210845 check this out. This should help. – user8210845 Aug 03 '17 at 15:36
  • 1
    @EduardoDennis wh00ps! C-:= – Mike Nakis Aug 03 '17 at 15:48
  • I've added the absolute path to no avail. Print writer is suppose to create a new file so I don't understand the "FileNotFound" part. Also here's the full error message: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException at Practice.main(Practice.java:9) – Raw_Data Aug 03 '17 at 16:17
  • no access denied message – Raw_Data Aug 03 '17 at 16:23
  • Awesome sauce @Dukeling!!!! throwing the exception allows me to create the empty file and write to it! thanx!!! Now my question is why does the exception need to be thrown when syntactically I should've been fine in the first place? – Raw_Data Aug 03 '17 at 16:33
  • [Why do we need exception handling?](https://stackoverflow.com/questions/21728173/why-do-we-need-exception-handling) – Bernhard Barker Aug 03 '17 at 16:37
  • Just for the curious, this is actually a duplicate of [Unresolved compilation: Unhandled exception type IOException](https://stackoverflow.com/questions/7811468/unresolved-compilation-unhandled-exception-type-ioexception) (my last comment seems to have been deleted for some reason). – Bernhard Barker Aug 03 '17 at 16:44

2 Answers2

0

Your code should look like this:

public class Practice
{
    public static void main(String[] args)
    {
        PrintWriter outputFile = new PrintWriter("C:\\Documents\\nums.txt");
        for(int i = 0; i < 100; i++)
        {
            outputFile.println(i + 1);
        }
        outputFile.close();
    }
}

This puts the nums text file into the documents folder.

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52
0

I get the same error only when the program attempts to create the file, under a path, that I do not have access permissions for.

Could you please check :
a. If the error message contains anything that says the 'Access is denied'
b. If so, please try mapping the file path to a folder/path that you have access permissions to.

Code snapshot + error details