java中創(chuàng)建寫入文件的6種方式詳解與源碼實例
在java中有很多的方法可以創(chuàng)建文件寫文件,你是否真的認真的總結過?下面筆者就幫大家總結一下java中創(chuàng)建文件的五種方法。
- Files.newBufferedWriter(Java 8)
- Files.write(Java 7 推薦)
- PrintWriter
- File.createNewFile
- FileOutputStream.write(byte[] b) 管道流
實際上不只這5種,通過管道流的排列組合,其實有更多種,但是筆者總結的這五種可以說是最常用及最佳實踐,
前提小知識
以前我在寫技術文章涉及到“流關閉”、“連接關閉”的時候,經常有人留言:“還寫技術文章,寫個流都不知道close()”,這種留言我遇到過無數(shù)回!
在本文中大量的使用到了try-with-resources語法,這個語法真的是很久的了,但是的確還有小伙伴不知道(知道的小伙伴就略過吧)。我還是說一下,下文中的管道流不是我沒close,是自動關閉close的。
try(管道流、連接等實現(xiàn)了Closeable接口的類){ //這里使用類對象操作 } //用try()包含起來,就不用在finally里面自己手動的去 Object.close()了,會自動的關閉
1. Java 8 Files.newBufferedWriter
java8 提供的newBufferedWriter可以創(chuàng)建文件,并向文件內寫入數(shù)據(jù)。可以通過追加寫模式,向文件內追加內容。
@Test void testCreateFile1() throws IOException { String fileName = "D:\\data\\test\\newFile.txt"; Path path = Paths.get(fileName); // 使用newBufferedWriter創(chuàng)建文件并寫文件 // 這里使用了try-with-resources方法來關閉流,不用手動關閉 try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { writer.write("Hello World -創(chuàng)建文件!!"); } //追加寫模式 try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND)){ writer.write("Hello World -字母哥!!"); } }
2. Java 7 Files.write
下面的這種方式Files.write,是筆者推薦的方式,語法簡單,而且底層是使用Java NIO實現(xiàn)的。同樣提供追加寫模式向已經存在的文件種追加數(shù)據(jù)。這種方式是實現(xiàn)文本文件簡單讀寫最方便快捷的方式。
@Test void testCreateFile2() throws IOException { String fileName = "D:\\data\\test\\newFile2.txt"; // 從JDK1.7開始提供的方法 // 使用Files.write創(chuàng)建一個文件并寫入 Files.write(Paths.get(fileName), "Hello World -創(chuàng)建文件!!".getBytes(StandardCharsets.UTF_8)); // 追加寫模式 Files.write( Paths.get(fileName), "Hello World -字母哥!!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); }
3. PrintWriter
PrintWriter是一個比較古老的文件創(chuàng)建及寫入方式,從JDK1.5就已經存在了,比較有特點的是:PrintWriter的println方法,可以實現(xiàn)一行一行的寫文件。
@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 -創(chuàng)建文件!!"); 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()方法的功能相對就比較純粹,只是創(chuàng)建文件不做文件寫入操作。 返回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("創(chuàng)建文件成功!"); } else { System.out.println("文件已經存在不需要重復創(chuàng)建"); } // 使用FileWriter寫文件 try (FileWriter writer = new FileWriter(file)) { writer.write("Hello World -創(chuàng)建文件!!"); } }
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 -創(chuàng)建文件!!"); bw.flush(); bw.close(); } }
6、文件工具類
import org.apache.commons.io.FileUtils; ???????public static void saveTxt(String filePath, String fileName, String data){ try{ File folder = new File(filePath); if(!folder.exists()){ folder.mkdirs(); } File file = new File(filePath + "/" + fileName); FileUtils.writeStringToFile(file,data,"UTF-8"); }catch(IOEException e){ log.error("error") } }
以上就是6種java創(chuàng)建寫入文件的方式,包括源碼實例,更多關于JAVA創(chuàng)建讀寫文件的方法請查看下面的相關鏈接
相關文章
Java數(shù)據(jù)導出功能之導出Excel文件實例
這篇文章主要介紹了Java數(shù)據(jù)導出功能之導出Excel文件實例,本文給出了jar包的下載地址,并給出了導出Excel文件代碼實例,需要的朋友可以參考下2015-06-06java讀取文件內容,解析Json格式數(shù)據(jù)方式
這篇文章主要介紹了java讀取文件內容,解析Json格式數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09spring cloud Hystrix斷路器的使用(熔斷器)
這篇文章主要介紹了spring cloud Hystrix斷路器的使用(熔斷器),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08