关于java File类中三种创建文件的理解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a2013126370/article/details/80966463

Stack Overflow的提问:

大致的意思是:File file = new File(pathname)是在此路径下创建了一个全新的文件(已有的文件则删除)吗?或者它指向和路径名匹配已创建的文件吗?

刚学习是我也是这样认为的,但是查了一下资料,发现我的认知是错误的,以下是一个解答,或许会有用。

File file = new File(pathname)查看官方文档也可知,只是创建了一个实例就是一个对象,对官方的解释:

总得来说,file并不是我们所理解的创建了一个实实在在的文件,而是 a new instance in memory of the File class。如果在相应的路径下,这个文件存在,那么file.exists()为true。否则的话,需要file.createNewFile()创建一个空文件。

如果对上面你不是特别理解的话,下面关于java 创建文件并读写的三种方法可能会给你提供帮助。

Java create new file

  • Using java.io.File Class
  • Using java.io.FileOutputStream Class
  • Using java.nio.file.Files from NIO

不啰嗦,直接上代码,不明白的地方可以查看官方API

Using java.io.File Class

File file = new File("c://temp//testFile1.txt");

//Create the file

if (file.createNewFile()){

System.out.println("File is created!");

}else{

System.out.println("File already exists.");

}


//Write Content

FileWriter writer = new FileWriter(file);

writer.write("Test data");

writer.close();

Using java.io.FileOutputStream Class

String data = "Test data";

FileOutputStream out = new FileOutputStream("c://temp//testFile2.txt");

out.write(data.getBytes());

out.close();

Using java.nio.file.Files from NIO

String data = "Test data";

Files.write(Paths.get("c://temp//testFile3.txt"), data.getBytes());

//or

List<String> lines = Arrays.asList("1st line", "2nd line");

Files.write(Paths.get("file6.txt"), lines, StandardCharsets.UTF_8,

        StandardOpenOption.CREATE, StandardOpenOption.APPEND);

完整程序

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.Arrays;

import java.util.List;



public class CreateNewFile

{

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

    {

        createFileUsingFileClass();

        createFileUsingFileOutputStreamClass();

        createFileIn_NIO();

    }



    private static void createFileUsingFileClass() throws IOException

    {

          File file = new File("c://temp//testFile1.txt");

  

          //Create the file

          if (file.createNewFile()){

            System.out.println("File is created!");

          }else{

            System.out.println("File already exists.");

          }

           

          //Write Content

          FileWriter writer = new FileWriter(file);

          writer.write("Test data");

          writer.close();

    }



    private static void createFileUsingFileOutputStreamClass() throws IOException

    {

        String data = "Test data";

        FileOutputStream out = new FileOutputStream("c://temp//testFile2.txt");

        out.write(data.getBytes());

        out.close();

    }



    private static void createFileIn_NIO()  throws IOException

    {

        String data = "Test data";

        Files.write(Paths.get("c://temp//testFile3.txt"), data.getBytes()); 

        //or

         

        List<String> lines = Arrays.asList("1st line", "2nd line");

        Files.write(Paths.get("file6.txt"), lines, StandardCharsets.UTF_8,

                StandardOpenOption.CREATE, StandardOpenOption.APPEND);

    }

}

参考文章:

https://stackoverflow.com/questions/19702659/about-file-file-new-filepath

https://howtodoinjava.com/core-java/io/how-to-create-a-new-file-in-java/

猜你喜欢

转载自blog.csdn.net/a2013126370/article/details/80966463