1,FileWriter類(字符輸出流類)
構造方法:
FileWriter fw = new FileWriter(String fileName);//創建字符輸出流類對象和已存在的文件相關聯。文件不存在的話,並創建。
如:FileWriter fw = new FileWriter("C:\\demo.txt");
FileWriter fw = new FileWriter(String fileName,boolean append);//創建字符輸出流類對象和已存在的文件相關聯,並設置該該流對文件的操作是否爲續寫。
如:FileWriter fw = new FileWriter("C:\\demo.txt",ture); //表示在fw對文件再次寫入時,會在該文件的結尾續寫,並不會覆蓋掉。
主要方法:
(1)void write(String str) //寫入字符串。當執行完此方法後,字符數據還並沒有寫入到目的文件中去。此時字符數據會保存在緩衝區中。 此時在使用刷新方法就可以使數據保存到目的文件中去。
(2) viod flush() //刷新該流中的緩衝。將緩衝區中的字符數據保存到目的文件中去。
(3)viod close() //關閉此流。在關閉前會先刷新此流的緩衝區。在關閉後,再寫入或者刷新的話,會拋IOException異常。
package filewriter;
import java.io.FileWriter;
import java.io.IOException;
public class Filewriter {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/**
* 創建一個可以往文件中寫入字符數據的字符流輸出流對象
* 創建時必須明確文件的目的地
* 如果文件不存在,這回自動創建。如果文件存在,則會覆蓋。
* 當路徑錯誤時會拋異常
*
* 當在創建時加入true參數,回實現對文件的續寫。
*/
FileWriter fw = new FileWriter("C:\\demo1.txt",false);
/**
* 調用該對象的write方法,向文件寫入字符。
*
* 其實寫入到了臨時存儲緩衝區中
*/
// fw.write("hello \r\nworld!");//windows中的換行爲\r\n unix下爲\r。
fw.write("aello"+LINE_SEPARATOR+"world!");
fw.write("hahaha");
/**
* 進行刷新,將字符寫到目的地中。
*/
// fw.flush();
/**
* 關閉流,關閉資源。在關閉前會調用flush方法 刷新緩衝區。關閉後在寫的話,會拋IOException
*/
fw.close();
}
}
關於FileWriter的的異常處理。
package filewriter;
import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionDemo {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("k:\\Demo.txt", true);
fw.write("hello" + LINE_SEPARATOR + "world!");
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("關閉失敗!");
}
}
}
}
2,FileReader類
構造方法
FileReader fr = new FileReader(String fileName);//使用帶有指定文件的String參數的構造方法。創建該輸入流對象。並關聯源文件。
主要方法
int read(); // 讀取單個字符。返回作爲整數讀取的字符,如果已達到流末尾,則返回 -1。
int read(char []cbuf);//將字符讀入數組。返回讀取的字符數。如果已經到達尾部,則返回-1。
void close();//關閉此流對象。釋放與之關聯的所有資源。
package Filereader;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
/**
* 創建讀取字符數據的流對象。
* 讀取路徑不正確時會拋 IOException
* 用以個讀取流對象關聯一個已存在文件。
*/
FileReader fr = new FileReader("demo.txt");
/**
* 用Reader中的read方法讀取字符。
*/
/*int ch = fr.read();
System.out.print((char)ch);
int ch1 = fr.read();
System.out.print((char)ch1);
int ch2 = fr.read();
System.out.print((char)ch2);*/
int ch = 0;
while((ch = fr.read()) != -1){
System.out.print((char)ch);
}
fr.close();
}
}
用FileReader 和 FileWriter 寫的複製文本文件的小程序。
package IOtest;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TxtCopy {
/**
* 將C:\\的myHeart.txt copy 到 D:\\下
*
* 首先創建Reader讀取數據數據的 讀取流對象。
*
* @throws FileNotFoundException
*/
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("C:\\my.txt");
fw = new FileWriter("D:\\you.txt");
//讀一個字符,寫一個字符方法
// int ch = 0;
//
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// }
char []buf = new char[1024];
int len = 0;
//讀一個數組大小,寫一個數組大小方法。
while((len = fr.read(buf)) != -1){
fw.write(buf, 0, len);
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (fr != null)
try {
fr.close();
} catch (Exception e2) {
throw new RuntimeException("關閉失敗!");
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("關閉失敗!");
}
}
}
}