寫文件在開發小工具時經常使用到,好比爬取某些網站的信息,數據量不是很大,保存到本地便可。固然若是會一些額外的技能,好比多線程,網絡之類的,小工具會更加有意思。java
這裏看下Java不一樣的寫文件方式:數組
把類中定義的方法信息,寫入文件bash
static String fileName = "/Users/aihe/tmp/writeFileDemo.txt";
static void writeFileWithBufferedWriter() throws IOException {
Method[] methods = WriteFileDemo.class.getDeclaredMethods();
String str = Arrays.toString(methods);
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(str);
writer.close();
}
複製代碼
追加信息到已經存在的文件:網絡
static void appendFileWithBufferedWriter() throws IOException {
// FileWriter的第二個參數表明是否追加
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
writer.append("追加信息");
writer.close();
}
複製代碼
PrintWriter能夠輸出格式化的信息到文件中。多線程
static void writingFileWithPrintWriter() throws IOException {
Method[] methods = WriteFileDemo.class.getDeclaredMethods();
String str = Arrays.toString(methods);
// 能夠使用FileWriter,BufferedWriter
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("當前類的方法信息: %s \n方法的個數:%d \n", str, methods.length);
printWriter.close();
}
複製代碼
用來寫入二進制數據到文件中,須要將String轉換爲bytes。app
static void writingFileWithFileOutputStream() throws IOException {
Method[] methods = WriteFileDemo.class.getDeclaredMethods();
String str = Arrays.toString(methods);
FileOutputStream outputStream = new FileOutputStream(fileName);
// 須要將String轉換爲bytes
byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
複製代碼
寫法如上dom
static void writingFileWithDataOutputStream()
throws IOException {
Method[] methods = WriteFileDemo.class.getDeclaredMethods();
String str = Arrays.toString(methods);
FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(str);
outStream.close();
// verify the results
String result;
FileInputStream fis = new FileInputStream(fileName);
DataInputStream reader = new DataInputStream(fis);
result = reader.readUTF();
reader.close();
System.out.println(result.equals(str));
}
複製代碼
想要寫入或者編輯一個已經存在的文件,而不是寫入一個全新的文件或者單純的追加,那麼咱們能夠使用RandomAccessFile。這個類能夠讓咱們寫入特定的位置,以下:工具
寫入中文的時候使用writeUTF方法,否則可能會亂碼網站
static void writeToPositionWithRAF(String filename, long position)
throws IOException {
RandomAccessFile writer = new RandomAccessFile(filename, "rw");
writer.seek(position);
//寫入中文的時候防止亂碼
writer.writeUTF("新內容");
writer.close();
}
複製代碼
在處理大文件的時候,FileChannel會比標準的io更快。ui
static void writeWithFileChannel() throws IOException {
RandomAccessFile stream = new RandomAccessFile(fileName, "rw");
FileChannel channel = stream.getChannel();
String value = WriteFileDemo.class.getSimpleName();
byte[] strBytes = value.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
buffer.put(strBytes);
buffer.flip();
channel.write(buffer);
stream.close();
channel.close();
}
複製代碼
Files是Java7引入的工具類,經過它,咱們能夠建立,移動,刪除,複製文件。目錄也是一種特殊的文件,對目錄也適用。固然也能夠用於讀寫文件
static void writeWithFiles()
throws IOException {
String str = "Hello";
Path path = Paths.get(fileName);
byte[] strToBytes = str.getBytes();
Files.write(path, strToBytes);
String read = Files.readAllLines(path).get(0);
System.out.println(str.equals(read));
}
複製代碼
操做文件的時候記得要關閉文件流,也能夠使用java7的try-with-resource語法。