java中創(chuàng)建、寫入文件的5種方式
在java中有很多的方法可以創(chuàng)建文件寫文件,你是否真的認(rèn)真的總結(jié)過?下面筆者就幫大家總結(jié)一下java中創(chuàng)建文件的五種方法。
Files.newBufferedWriter(Java 8) Files.write(Java 7 推薦) PrintWriter File.createNewFile FileOutputStream.write(byte[] b) 管道流實際上不只這5種,通過管道流的排列組合,其實有更多種,但是筆者總結(jié)的這五種可以說是最常用及最佳實踐,
前提小知識
以前我在寫技術(shù)文章涉及到“流關(guān)閉”、“連接關(guān)閉”的時候,經(jīng)常有人留言:“還寫技術(shù)文章,寫個流都不知道close()”,這種留言我遇到過無數(shù)回!在本文中大量的使用到了try-with-resources語法,這個語法真的是很久的了,但是的確還有小伙伴不知道(知道的小伙伴就略過吧)。我還是說一下,下文中的管道流不是我沒close,是自動關(guān)閉close的。
try(管道流、連接等實現(xiàn)了Closeable接口的類){ //這里使用類對象操作}//用try()包含起來,就不用在finally里面自己手動的去 Object.close()了,會自動的關(guān)閉
1. Java 8 Files.newBufferedWriter
java8 提供的newBufferedWriter可以創(chuàng)建文件,并向文件內(nèi)寫入數(shù)據(jù)??梢酝ㄟ^追加寫模式,向文件內(nèi)追加內(nèi)容。
@Testvoid testCreateFile1() throws IOException { String fileName = 'D:datatestnewFile.txt'; Path path = Paths.get(fileName); // 使用newBufferedWriter創(chuàng)建文件并寫文件 // 這里使用了try-with-resources方法來關(guān)閉流,不用手動關(guān)閉 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)的。同樣提供追加寫模式向已經(jīng)存在的文件種追加數(shù)據(jù)。這種方式是實現(xiàn)文本文件簡單讀寫最方便快捷的方式。
@Testvoid testCreateFile2() throws IOException { String fileName = 'D:datatestnewFile2.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就已經(jīng)存在了,比較有特點的是:PrintWriter的println方法,可以實現(xiàn)一行一行的寫文件。
@Testvoid testCreateFile3() throws IOException { String fileName = 'D:datatestnewFile3.txt'; // JSD 1.5開始就已經(jīng)存在的方法 try (PrintWriter writer = new PrintWriter(fileName, 'UTF-8')) { writer.println('Hello World -創(chuàng)建文件!!'); writer.println('Hello World -字母哥!!'); } // Java 10進(jìn)行了改進(jìn),支持使用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表示文件已經(jīng)存在.可以配合FileWriter 來完成文件的寫操作。
@Testvoid testCreateFile4() throws IOException { String fileName = 'D:datatestnewFile4.txt'; File file = new File(fileName); // 返回true表示文件成功 // false 表示文件已經(jīng)存在 if (file.createNewFile()) { System.out.println('創(chuàng)建文件成功!'); } else { System.out.println('文件已經(jīng)存在不需要重復(fù)創(chuàng)建'); } // 使用FileWriter寫文件 try (FileWriter writer = new FileWriter(file)) { writer.write('Hello World -創(chuàng)建文件!!'); }}
5.最原始的管道流方法
最原始的方式就是使用管道流嵌套的方法,但是筆者覺得這種方法歷久彌新,使用起來非常靈活。你想去加上Buffer緩沖,你就嵌套一個BufferedWriter,你想去向文件中寫java對象你就嵌套一個ObjectOutputStream。但歸根結(jié)底要用到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 -創(chuàng)建文件!!'); bw.flush(); }}
以上就是java中創(chuàng)建、寫入文件的5種方式的詳細(xì)內(nèi)容,更多關(guān)于Java 創(chuàng)建、寫入文件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
