更新時間:2020-07-17 16:32:50 來源:動力節(jié)點 瀏覽2361次
字符流
一個字符一個字符的讀
mac系統(tǒng)下,一個中文字符占3個字節(jié)默認使用UTF-8的編碼表(通用的編碼表)
Windows系統(tǒng)下,一個中文字符占2個字節(jié)默認使用的GBK的編碼表(簡體中文)
注意:只能操作文本(不能寫圖片、音頻、視頻)
字符輸出流
Writer(所有字符輸出流的父類?抽象類)
????FileWriter
????構(gòu)造方法(綁定寫入的路徑):
????????????????文件
????????????????字符串
????注意:字符輸出流在寫入文件的時候需要調(diào)用刷新方法
代碼實例:
????????FileWriter?fWriter?=?new?FileWriter("/Users/lanou/Desktop/level/haha.txt");
????????fWriter.write(100);
????????//?每次寫入?最好都刷新一次
????????fWriter.flush();
????????//?字符數(shù)組寫入
????????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呃呃呃\n");
????????fWriter.flush();
????????fWriter.write("白日依山盡",?0,?2);
????????//?關(guān)閉資源前?會刷新
????????fWriter.close();
字符輸入流
Reader(所有字符輸入流的父類 抽象類)
讀的時候不能直接讀取字符串,因為字符串很難界定到哪結(jié)束,不太容易判斷一個字符串
循環(huán)讀取:
FileReader fr = new FileReader("/Users/lanou/Desktop/level/haha.txt");
int num = 0;
while ((num = fr.read()) != -1) {
System.out.println((char)num);
}
char[] c = new char[1024];
while ((num = fr.read(c)) != -1) {
System.out.println(new String(c, 0, num));
}
fr.close();
利用字符流復(fù)制文件
public class Demo {
public static void main(String[] args) {
File file1 = new File("/Users/lanou/Desktop/level/9.jpg");
File file2 = new File("/Users/lanou/Desktop/XTest/9.jpg");
name(file1, file2);
}
public static void name(File file1,File file2) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file1);
fw = new FileWriter(file2);
int len = 0;
char[] c = new char[1024];
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到文件");
} catch (IOException e) {
throw new RuntimeException("文件復(fù)制失敗");
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
throw new RuntimeException("關(guān)閉資源失敗");
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
throw new RuntimeException("關(guān)閉資源失敗");
}
}
}
}
}
轉(zhuǎn)換流
OutputStreamWriter(字符流轉(zhuǎn)向字節(jié)流)
作用:可以使用不同編碼格式寫入
需要使用FileOutPutStream類
InputStreamReader(字節(jié)流轉(zhuǎn)向字符流)
作用:可以讀取不同編碼格式的文件
需要使用FileInputStream類
public class Demo {
public static void main(String[] args) throws IOException {
getUTF8();
getGBK();
readerGBK();
readUTF8();
}
// 利用轉(zhuǎn)換流寫文件 OutputStreamWriter 默認uft8寫
public static void getUTF8() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/utf8.txt");
// 創(chuàng)建轉(zhuǎn)換流 字符流轉(zhuǎn)向字節(jié)流
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 寫文件
osw.write("SC");
// 只關(guān)閉外層的流
osw.close();
}
// 使用GBK的編碼寫入文件 利用轉(zhuǎn)換流
public static void getGBK() throws IOException{
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/gbk.txt");
// 構(gòu)建轉(zhuǎn)換流 傳入編碼格式(編碼格式的字符串 忽略大小寫)
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
osw.write("SC");
osw.close();
}
// 使用GBK的編碼讀取文件 利用轉(zhuǎn)換流
public static void readerGBK() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/level/gbk.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
int len = 0;
char[] c = new char[1024];
while ((len = isr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
isr.close();
}
// 使用uft8的編碼讀取文件 利用轉(zhuǎn)換流
public static void readUTF8() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/level/utf8.txt");
InputStreamReader isr = new InputStreamReader(fis);
int len = 0;
char[] c = new char[1024];
while ((len = isr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
isr.close();
}
}
以上就是動力節(jié)點java培訓(xùn)機構(gòu)的小編針對“Java中字符流之輸入,輸出流以及轉(zhuǎn)換流”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
相關(guān)閱讀