更新時間:2020-07-17 16:32:50 來源:動力節點 瀏覽2283次
字符流
一個字符一個字符的讀
mac系統下,一個中文字符占3個字節默認使用UTF-8的編碼表(通用的編碼表)
Windows系統下,一個中文字符占2個字節默認使用的GBK的編碼表(簡體中文)
注意:只能操作文本(不能寫圖片、音頻、視頻)
字符輸出流
Writer(所有字符輸出流的父類?抽象類)
????FileWriter
????構造方法(綁定寫入的路徑):
????????????????文件
????????????????字符串
????注意:字符輸出流在寫入文件的時候需要調用刷新方法
代碼實例:
????????FileWriter?fWriter?=?new?FileWriter("/Users/lanou/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呃呃呃\n");
????????fWriter.flush();
????????fWriter.write("白日依山盡",?0,?2);
????????//?關閉資源前?會刷新
????????fWriter.close();
字符輸入流
Reader(所有字符輸入流的父類 抽象類)
讀的時候不能直接讀取字符串,因為字符串很難界定到哪結束,不太容易判斷一個字符串
循環讀取:
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();
利用字符流復制文件
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("文件復制失敗");
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
}
}
}
}
}
轉換流
OutputStreamWriter(字符流轉向字節流)
作用:可以使用不同編碼格式寫入
需要使用FileOutPutStream類
InputStreamReader(字節流轉向字符流)
作用:可以讀取不同編碼格式的文件
需要使用FileInputStream類
public class Demo {
public static void main(String[] args) throws IOException {
getUTF8();
getGBK();
readerGBK();
readUTF8();
}
// 利用轉換流寫文件 OutputStreamWriter 默認uft8寫
public static void getUTF8() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/utf8.txt");
// 創建轉換流 字符流轉向字節流
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 寫文件
osw.write("SC");
// 只關閉外層的流
osw.close();
}
// 使用GBK的編碼寫入文件 利用轉換流
public static void getGBK() throws IOException{
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/level/gbk.txt");
// 構建轉換流 傳入編碼格式(編碼格式的字符串 忽略大小寫)
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
osw.write("SC");
osw.close();
}
// 使用GBK的編碼讀取文件 利用轉換流
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的編碼讀取文件 利用轉換流
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();
}
}
以上就是動力節點java培訓機構的小編針對“Java中字符流之輸入,輸出流以及轉換流”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業老師隨時為你服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習