4

I am trying to create a new file and print data to said file using the printwriter class.

My code looks like this

File Fileright = new File("C:\\GamesnewOrder.txt"); 

PrintWriter pw = new PrintWriter(Fileright);

for(int i =0;i<=Games2.length-1;i++)
{
    pw.println(Games2[i]);

}

pw.close();

I do have the main method with a throwsIOException.

The error java.iofilenotfound exception keeps appearing at the line where I am creating the printwriter. So is the printwriter not creating the file?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Brandon Nolan
  • 125
  • 1
  • 2
  • 10

2 Answers2

3

the code works for me.

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

  public class NewClass {

     void n() throws FileNotFoundException {
             File Fileright = new File("/home/ubuntu/Documents/f.txt");

             PrintWriter pw = new PrintWriter(Fileright);

             for (int i = 0; i <= 3; i++) {
                pw.println(i);
                System.out.println(i);

             }

             pw.close();
      }

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

             new NewClass().n();
      }

}

output:(in file: /home/ubuntu/Documents/f.txt)

0
1
2
3

novice
  • 31
  • 1
2

FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

Please check the file Permission, you may use canRead() , canWrite() to check that, but it may not be sufficient enough.

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149