是否可以在java中关闭源文件(file.close();)?

sur*_*uru 0 java notepad file

我正在创建一个新文件

File f = new File(file_path);
Run Code Online (Sandbox Code Playgroud)

然后程序结束我可以关闭文件对象或文件?

f.close();
Run Code Online (Sandbox Code Playgroud)

否则有一种方法可以关闭文件??

     public class etest2read {
     public static void main(String[] args) throws IOException {
    File dir = new File("input");

    String source = dir.getCanonicalPath() + File.separator + "TestFile.txt";
    //String TestFileone = dir.getCanonicalPath() + File.separator + "TestFileone.txt";

    File fin = new File(source);
    FileInputStream fis = new FileInputStream(fin);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    System.out.println("file/folder: "+fin.getAbsolutePath());
    System.out.println("file/folder: "+dir.getCanonicalPath());
    System.out.println("file/folder: "+fin.lastModified());


    String strLine;

     //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
   // Print the content on the console
    System.out.println (strLine);
    }

  //Close the input stream
    br.close();
    System.out.println("Closed Buffered Reader");
    fis.close();
    System.out.println("Closed File Input Stream");

    fin.close(); // providing the error
       }
   }
Run Code Online (Sandbox Code Playgroud)

Bur*_*ard 5

不,这是不可能的.

文件是文件或目录路径名的抽象表示.您不打开文件,只打开该文件上的Stream或Reader.