关于spring:总结java中创建并写文件的5种方式

9次阅读

共计 3100 个字符,预计需要花费 8 分钟才能阅读完成。

在 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 能够创立文件,并向文件内写入数据。能够通过追加写模式,向文件内追加内容。

@Test
void 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 实现的。同样提供追加写模式向曾经存在的文件种追加数据。这种形式是实现文本文件简略读写最方便快捷的形式。

@Test
void 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 办法,能够实现一行一行的写文件。

@Test
void 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 来实现文件的写操作。

@Test
void 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。

@Test
void 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 深入浅出系列》
正文完
 0