java中創建、寫入文件的5種方式
在java中有很多的方法可以創建文件寫文件,你是否真的認真的總結過?下面筆者就幫大家總結一下java中創建文件的五種方法。
Files.newBufferedWriter(Java 8) Files.write(Java 7 推薦) PrintWriter File.createNewFile 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:datatestnewFile.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:datatestnewFile2.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:datatestnewFile3.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:datatestnewFile4.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:datatestnewFile5.txt'; try(FileOutputStream fos = new FileOutputStream(fileName); OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw);){ bw.write('Hello World -創建文件!!'); bw.flush(); }}
以上就是java中創建、寫入文件的5種方式的詳細內容,更多關于Java 創建、寫入文件的資料請關注好吧啦網其它相關文章!
相關文章:
