上一篇寫了java讀取文件的各類操做姿式,這裏也補一個寫文件的工具類,比較簡單java
java讀寫文件的IO流分兩大類,字節流和字符流,基類分別是字符:Reader和Writer;字節:InputStream和OutPutStreamgit
字符流分爲FileReader和FileWrtier,這兩個的父類是InputStreamReader和OutStreamWrtier工具
字節流分爲FileInputStream和FileOutPutStream性能
繼承關係表測試
Reader->InputStreamReader->FileReader Reader->BufferedReader Writer->OutputStreamWriter->FileWriter Writer->BufferedWriter InputStream->FileInputStream。FileInputStream 用於讀取諸如圖像數據之類的原始字節流。要讀取字符流,請考慮使用 FileReader。 InputStream->FilterInputStream->BufferedInputStream OutputStream->FileOutputStream。FileOutputStream 用於寫入諸如圖像數據之類的原始字節的流。要寫入字符流,請考慮使用 FileWriter OutputStream->FilterOutputStream->BufferedOutputStream
通常使用流程ui
File file = new File("xxx.txt");
OutputStream fr = new FileOutputStream(file);
BufferedOutputStream br = new BufferedOutputStream(fr);
br.write(xxx)
寫文件,能夠按字節方式寫入,也能夠按照字符方式寫入文件,寫文件,包括是不是追加寫,編碼方式等this
寫文件以前, 文件能夠不存在,但文件所在的目錄必須有,因此就有個生成目錄的方法了編碼
提供了一個工具類 DirUtil
, 能夠遞歸生成path
路徑對應的全部目錄(前提是有權限).net
/** * 遞歸建立文件夾 * * @param file 由目錄建立的file對象 * @throws FileNotFoundException */ public static void mkDir(File file) throws FileNotFoundException { if (file == null) { throw new FileNotFoundException(); } if (file.getParentFile().exists()) { if (file.exists()) { // 目錄存在, 則直接返回 return; } if (!file.mkdir()) { // 不存在, 則建立 throw new FileNotFoundException(); } } else { mkDir(file.getParentFile()); // 建立父目錄 if (!file.exists() && !file.mkdir()) { throw new FileNotFoundException(); } } }
提供了兩種寫文件的方式,字節流輸出和字符流輸出,其中字符流輸出時,制定了編碼格式爲UTF-8
, 這個工具的實現方式與讀的工具類不太相同,先看下代碼結構code
/** * 寫文件工具類 * <p/> * Created by yihui on 2017/5/17. */ public class FileWriteUtil { public enum WriteType { BUFFER, WRITER } private Object output; private WriteType currentType; public static FileWriteUtil newInstance(WriteType writeType, String filename, boolean isAppend) throws IOException { return new FileWriteUtil(writeType, filename, isAppend); } private FileWriteUtil(WriteType writeType, String filename, boolean isAppend) throws IOException { currentType = writeType; if (writeType == WriteType.BUFFER) { output = new BufferedOutputStream(new FileOutputStream(filename, isAppend)); } else if (writeType == WriteType.WRITER) { output = new OutputStreamWriter(new FileOutputStream(filename, isAppend), Charset.forName("UTF-8")); } } public FileWriteUtil write(String data) throws IOException { if (currentType == WriteType.BUFFER) { ((BufferedOutputStream) output).write(data.getBytes()); ((BufferedOutputStream) output).flush(); } else { ((OutputStreamWriter) output).write(data); ((OutputStreamWriter) output).flush(); } return this; } public void close() throws IOException { if (currentType == WriteType.BUFFER) { ((BufferedOutputStream) output).close(); } else { ((OutputStreamWriter) output).close(); } } }
com.hust.hui.quicksilver.file.FileWriteUtil.newInstance(xxx)
方式來獲取實例對象,而後持有這個對象進行寫入操做write()
方法返回對象自引用, 所以支持相似 builder模式
的輸出方式代碼結構比較簡單,實現 + 測試都是一目瞭然,直接貼出代碼和結果
@Test public void testBufWrite() throws IOException { FileWriteUtil fileWrite = FileWriteUtil.newInstance(FileWriteUtil.WriteType.BUFFER, "bufWrite.txt", false); try { fileWrite.write("hello world") .write("\n") .write("你好😄《-表情符》"); } finally { fileWrite.close(); } } @Test public void testWriterWrite() throws IOException { FileWriteUtil fileWrite = FileWriteUtil.newInstance(FileWriteUtil.WriteType.WRITER, "writerWrite.txt", false); try { fileWrite.write("hello world") .write("\n") .write("你好😄《-表情符》"); } finally { fileWrite.close(); } }
測試用例演示
源碼直通車: https://git.oschina.net/liuyueyi/quicksilver/tree/master/silver-file?dir=1&filepath=silver-file
讀文件連接:java之的讀取文件大全
反射生成對象博文: java之經過反射生成並初始化對象