How to use PrintWriter and File classes in Java?

2019-01-03 05:57发布

I am trying to understand PrintWriter for a small program I'm making, and I cant seem to get java to make the file and then write on it. When I execute the program below it gives me a Filenotfoundexeption error on line 9. It also fails to make the file in the directory that I specified. I am new to this so please try and keep the answers simple. I am using Eclipse.

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

public class Testing {

  public static void main(String[] args) {

    File file = new File ("C:/Users/Me/Desktop/directory/file.txt");
    PrintWriter printWriter = new PrintWriter ("file.txt");
    printWriter.println ("hello");
    printWriter.close ();       
  }
}

11条回答
Lonely孤独者°
2楼-- · 2019-01-03 06:19
import java.io.File;
import java.io.PrintWriter;

public class Testing 
{
    public static void main(String[] args) 
    {

        File file = new File("C:/Users/Me/Desktop/directory/file.txt");

        PrintWriter printWriter = null;

        try
        {
            printWriter = new PrintWriter(file);
            printWriter.println("hello");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if ( printWriter != null ) 
            {
                printWriter.close();
            }
        }
    }
}
查看更多
三岁会撩人
3楼-- · 2019-01-03 06:22

Pass the File object to the constructor PrintWriter(File file):

PrintWriter printWriter = new PrintWriter(file);
查看更多
地球回转人心会变
4楼-- · 2019-01-03 06:22
import java.io.*;

public class test{
    public static void main(Strings []args){

       PrintWriter pw = new PrintWriter(new file("C:/Users/Me/Desktop/directory/file.txt"));

       pw.println("hello");
       pw.close
    }

}

查看更多
我命由我不由天
5楼-- · 2019-01-03 06:23

If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.

As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see

FileNotFoundException - If the given string 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

You should try creating a path for the folder it contains before:

File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();

PrintWriter printWriter = new PrintWriter(file);
查看更多
疯言疯语
6楼-- · 2019-01-03 06:27

Java doesn't normally accept "/" to use in defining a file directory, so try this:

 File file = new File ("C:\\Users\\user\\workspace\\FileTester\\myFile.txt");

If the file doesn't exist do:

 try {
      file.createNewFile();
 }
 catch (IOException e) {
 e.printStackTrace();
 }
查看更多
老娘就宠你
7楼-- · 2019-01-03 06:38

You should have a clear idea of exceptions in java. In java there are checked exceptions and unchecked exceptions.

Checked exceptions are checked (not thrown,just checked) by the compiler at Compile time for the smooth execution of the program at run time.

NOTE: And in our program if their is a chance that a checked exception will rise, then we should handle that checked exception either by try catch or by throws key word.Otherwise we will get a compile time Error:

CE:Unexpected Exception java.io.FileNotFoundException;must be caught or declared to be thrown.

How to resolve: 1.Put your code in try catch block:

2.use throws keyword as shown by other guys above.

Advice:Read more about Exceptions.(I personally love this topic)

查看更多
登录 后发表回答