在java中有很多的办法能够创立文件写文件,你是否真的认真的总结过?上面笔者就帮大家总结一下java中创立文件的五种办法。

  1. Files.newBufferedWriter(Java 8)
  2. Files.write(Java 7 举荐)
  3. PrintWriter
  4. File.createNewFile
  5. FileOutputStream.write(byte[] b) 管道流
实际上不只这5种,通过管道流的排列组合,其实有更多种,然而笔者总结的这五种能够说是最罕用及最佳实际,

前提小常识

以前我在写技术文章波及到“流敞开”、“连贯敞开”的时候,常常有人留言:“还写技术文章,写个流都不晓得close()”,这种留言我遇到过无数回!
在本文中大量的应用到了try-with-resources语法,这个语法真的是很久的了,然而确实还有小伙伴不晓得(晓得的小伙伴就略过吧)。我还是说一下,下文中的管道流不是我没close,是主动敞开close的。

try(管道流、连贯等实现了Closeable接口的类){    //这里应用类对象操作}//用try()蕴含起来,就不必在finally外面本人手动的去 Object.close()了,会主动的敞开

1. Java 8 Files.newBufferedWriter

java8 提供的newBufferedWriter能够创立文件,并向文件内写入数据。能够通过追加写模式,向文件内追加内容。

@Testvoid testCreateFile1() throws IOException {   String fileName = "D:\\data\\test\\newFile.txt";   Path path = Paths.get(fileName);   // 应用newBufferedWriter创立文件并写文件   // 这里应用了try-with-resources办法来敞开流,不必手动敞开   try (BufferedWriter writer =                   Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {      writer.write("Hello World -创立文件!!");   }   //追加写模式   try (BufferedWriter writer =                Files.newBufferedWriter(path,                        StandardCharsets.UTF_8,                        StandardOpenOption.APPEND)){       writer.write("Hello World -字母哥!!");   }}

2. Java 7 Files.write

上面的这种形式Files.write,是笔者举荐的形式,语法简略,而且底层是应用Java NIO实现的。同样提供追加写模式向曾经存在的文件种追加数据。这种形式是实现文本文件简略读写最方便快捷的形式。

@Testvoid testCreateFile2() throws IOException {   String fileName = "D:\\data\\test\\newFile2.txt";   // 从JDK1.7开始提供的办法   // 应用Files.write创立一个文件并写入   Files.write(Paths.get(fileName),               "Hello World -创立文件!!".getBytes(StandardCharsets.UTF_8));   // 追加写模式   Files.write(         Paths.get(fileName),         "Hello World -字母哥!!".getBytes(StandardCharsets.UTF_8),         StandardOpenOption.APPEND);}

3. PrintWriter

PrintWriter是一个比拟古老的文件创建及写入形式,从JDK1.5就曾经存在了,比拟有特点的是:PrintWriter的println办法,能够实现一行一行的写文件。

@Testvoid testCreateFile3() throws IOException {   String fileName = "D:\\data\\test\\newFile3.txt";   // JSD 1.5开始就曾经存在的办法   try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {      writer.println("Hello World -创立文件!!");      writer.println("Hello World -字母哥!!");   }   // Java 10进行了改良,反对应用StandardCharsets指定字符集   /*try (PrintWriter writer = new PrintWriter(fileName, StandardCharsets.UTF_8)) {      writer.println("first line!");      writer.println("second line!");   } */}

4. File.createNewFile()

createNewFile()办法的性能绝对就比拟纯正,只是创立文件不做文件写入操作。 返回true示意文件胜利,返回 false示意文件曾经存在.能够配合FileWriter 来实现文件的写操作。

@Testvoid testCreateFile4() throws IOException {   String fileName = "D:\\data\\test\\newFile4.txt";   File file = new File(fileName);   // 返回true示意文件胜利   // false 示意文件曾经存在   if (file.createNewFile()) {      System.out.println("创立文件胜利!");   } else {      System.out.println("文件曾经存在不须要反复创立");   }   // 应用FileWriter写文件   try (FileWriter writer = new FileWriter(file)) {      writer.write("Hello World -创立文件!!");   }}

5.最原始的管道流办法

最原始的形式就是应用管道流嵌套的办法,然而笔者感觉这种办法历久弥新,应用起来非常灵活。你想去加上Buffer缓冲,你就嵌套一个BufferedWriter,你想去向文件中写java对象你就嵌套一个ObjectOutputStream。但归根结底要用到FileOutputStream。

@Testvoid testCreateFile5() throws IOException {   String fileName = "D:\\data\\test\\newFile5.txt";   try(FileOutputStream fos = new FileOutputStream(fileName);      OutputStreamWriter osw = new OutputStreamWriter(fos);      BufferedWriter bw = new BufferedWriter(osw);){      bw.write("Hello World -创立文件!!");      bw.flush();   }}

欢送关注我的博客,外面有很多精品合集

  • 本文转载注明出处(必须带连贯,不能只转文字):字母哥博客。

感觉对您有帮忙的话,帮我点赞、分享!您的反对是我不竭的创作能源! 。另外,笔者最近一段时间输入了如下的精品内容,期待您的关注。

  • 《手摸手教你学Spring Boot2.0》
  • 《Spring Security-JWT-OAuth2一本通》
  • 《实战前后端拆散RBAC权限管理系统》
  • 《实战SpringCloud微服务从青铜到王者》
  • 《VUE深入浅出系列》