字符流
一個字符字符的讀 只能用來操作文本(不能寫其他格式)
字符輸出流(Writer)
寫入字符流的抽象類
實現子類 FileWriter
代碼示例:
public class Demo04 {
public static void main(String[] args) throws IOException {
FileWriter fWriter = new FileWriter("/Users/james/Desktop/level/haha.txt");
fWriter.write(100);
// 注意 字符輸出流 在寫入文件的時候需要調用刷新方法
// 建議 每次寫入最好都刷新一次
fWriter.flush();
char[] c = {'l','o','n','g'};
fWriter.write(c);
fWriter.flush();
fWriter.write(c, 1,3);
fWriter.flush();
// 使用字符串直接寫入
fWriter.write("抽刀斷水水更流\n");
fWriter.flush();
fWriter.write("借酒消愁愁更愁\n");
fWriter.flush();
fWriter.write("百日依山盡", 1, 2);
// 關閉資源前 刷新
fWriter.close();
}
}
字符輸入流(Reader)
用於讀取字符流的抽象類
實現子類:FileReader
代碼示例:
public class Demo05 {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("/Users/james/Desktop/level/haha.txt");
// 循環讀取
char[] bs = new char[1024];
int len = 0;
while ((len = reader.read(bs)) != -1) {
System.out.println(new String(bs, 0, len));
}
reader.close();
}
}
字符流中輸出流中可以直接寫字符串類型 但是輸入流纔行 這是爲什麼呢?
因爲字符串類型很難判斷什麼時候結束 因此輸入流不能直接讀字符串
例題聯繫:
利用字符流複製文件
public class Demo06 {
public static void main(String[] args) {
File file = new File("/Users/james/Desktop/level/haha.txt");
File file2 = new File("/Users/james/Desktop/level/ha.txt");
copyFileTxt(file, file2);
}
// 字符流複製文件
public static void copyFileTxt(File src ,File dest) {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader(src);
writer = new FileWriter(dest);
char[] c = new char[1024];
int len = 0;
while ((len = reader.read(c)) != -1) {
writer.write(c, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到文件");
} catch (IOException e) {
throw new RuntimeException("編譯異常");
}finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException("資源關閉異常");
}finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
throw new RuntimeException("資源關閉異常");
}
}
}
}
}
轉換流
OutputStreamWriter(字符流轉向字節流)
字符流轉向字節流 可以使用不同編碼格式寫入 需要使用到FileOutStream類
InputStreamReader(字節流轉向字符流)
可以讀取不同編碼格式的文件 需要使用到 FileInputStream類
代碼示例:
public class Demo07 {
public static void main(String[] args) throws IOException {
//getUTF8();
// getGBK();
// readUTF8();
readGBK();
}
// 利用轉換流寫文件 OutputStreamWriter 默認UTF-8寫
public static void getUTF8() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/utf-8.txt");
// 創建轉換流 字符流轉向字節流
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 寫文件
osw.write("雪中智代雨中杏,櫻下小渚花田汐");
// 關閉資源(只關閉外層流 就可以)
osw.close();
}
// 利用轉換流寫文件 OutputStreamWriter 使用GBK
public static void getGBK() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/GBK.txt");
// 構建轉換流 傳入編碼格式(編碼格式的字符串 忽略大小寫)
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
osw.write("雪中智代雨中杏,櫻下小渚花田汐");
osw.close();
}
public static void readUTF8() throws IOException {
FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/utf-8.txt");
InputStreamReader isr = new InputStreamReader(fis);
int len = 0;
char[] b = new char[1024];
while ((len = isr.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
isr.close();
}
public static void readGBK() throws IOException {
FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/GBK.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
int len = 0;
char[] b = new char[1024];
while ((len = isr.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
isr.close();
}
}