Writer 是寫入字符流的抽象類。
站在用戶的角度思考問題,與客戶深入溝通,找到襄州網(wǎng)站設(shè)計與襄州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋襄州地區(qū)。
OutputStreamWriter(OutputStream out):輸出轉(zhuǎn)換流,創(chuàng)建使用默認(rèn)字符編碼的OutputStreamWriter。
OutputStreamWriter(OutputStream out, String charsetName):創(chuàng)建使用指定字符編碼的OutputStreamWriter。
字符流的底層還是用字節(jié)流進(jìn)行讀寫的,字符流僅僅是做了字節(jié)和字符的轉(zhuǎn)換。
二、字符流寫數(shù)據(jù)Reader是用于讀取字符流的抽象類。
InputStreamReader(InputStream in):輸入轉(zhuǎn)換流,創(chuàng)建默認(rèn)字符集的InputStreamReader。
InputStreamReader(InputStream in, String charsetName):創(chuàng)建使用指定字符編碼的InputStreamReader。
方法 | 說明 |
---|---|
publicvoid write(int c) | 寫出一個字符 |
publicvoid write(char[] cbuf) | 寫出字符數(shù)組 |
publicvoid write(char[] cbuf, int off, int len) | 寫出字符數(shù)組cbuf中,從off開始,共len個字符 |
publicvoid write(String str) | 寫出字符串 |
publicvoid write(String str, int off, int len) | 寫出字符串,從off開始,共len個字符 |
eg:
//publicvoid write(int c) 寫出一個字符
public static void main(String[] args) {OutputStreamWriter outputStreamWriter = null;
try {outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
outputStreamWriter.write('a');
outputStreamWriter.write('b');
outputStreamWriter.write('c');
outputStreamWriter.write('貓');
} catch (IOException e) {e.printStackTrace();
} finally {if (outputStreamWriter != null) {try {outputStreamWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
//publicvoid write(char[] cbuf, int off, int len) 寫出字符數(shù)組cbuf中,從off開始,共len個字符
public static void main(String[] args) {OutputStreamWriter outputStreamWriter = null;
try {outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
char[] chars = {'a','b','c','貓'};
outputStreamWriter.write(chars,2,2);
} catch (IOException e) {e.printStackTrace();
} finally {if (outputStreamWriter != null) {try {outputStreamWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
//publicvoid write(char[] cbuf) 寫出字符數(shù)組
public static void main(String[] args) {OutputStreamWriter outputStreamWriter = null;
try {outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
char[] chars = {'a','b','c','貓'};
outputStreamWriter.write(chars);
} catch (IOException e) {e.printStackTrace();
} finally {if (outputStreamWriter != null) {try {outputStreamWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
//publicvoid write(String str) 寫出字符串
public static void main(String[] args) {OutputStreamWriter outputStreamWriter = null;
try {outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
outputStreamWriter.write("好好學(xué)習(xí),天天向上");
} catch (IOException e) {e.printStackTrace();
}finally {if(outputStreamWriter != null){try {outputStreamWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
//publicvoid write(String str, int off, int len) 寫出字符串,從off開始,共len個字符
public static void main(String[] args) {OutputStreamWriter outputStreamWriter = null;
try {outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
outputStreamWriter.write("好好學(xué)習(xí),天天向上", 4, 4);
} catch (IOException e) {e.printStackTrace();
}finally {if(outputStreamWriter != null){try {outputStreamWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
三、字符流讀數(shù)據(jù)方法 | 說明 |
---|---|
int read() | 一次讀一個字符數(shù)據(jù) |
int read(char[] cbuf) | 一次讀一個字符數(shù)組數(shù)據(jù) |
eg:
//int read() 一次讀一個字符數(shù)據(jù)
public static void main(String[] args) {InputStreamReader inputStreamReader = null;
try {inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"), "UTF-8");
System.out.println((char)inputStreamReader.read());
System.out.println((char)inputStreamReader.read());
System.out.println((char)inputStreamReader.read());
} catch (IOException e) {e.printStackTrace();
}finally {if(inputStreamReader != null){try {inputStreamReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
//int read(char[] cbuf) 一次讀一個字符數(shù)組數(shù)據(jù)
public static void main(String[] args) {InputStreamReader inputStreamReader = null;
try {inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
char[] chars = new char[1024];
int length;
while ((length = inputStreamReader.read(chars)) != -1){System.out.println(new String(chars,0,length));
}
} catch (IOException e) {e.printStackTrace();
}finally {if(inputStreamReader != null){try {inputStreamReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
四、字符流拷貝文本文件eg:
public static void main(String[] args) { InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try { inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt"));
char[] chars = new char[1024];
int length;
while ((length = inputStreamReader.read(chars)) != -1){ outputStreamWriter.write(chars,0, length);
}
} catch (IOException e) { e.printStackTrace();
}finally { if(inputStreamReader != null){ try { inputStreamReader.close();
} catch (IOException e) { e.printStackTrace();
}
}
if(outputStreamWriter != null){ try { outputStreamWriter.close();
} catch (IOException e) { e.printStackTrace();
}
}
}
}
五、FileWriter和FileReader
eg:
public static void main(String[] args) {FileReader fileReader = null;
FileWriter fileWriter = null;
try {fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt");
fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt");
char[] chars = new char[1024];
int length;
while ((length = fileReader.read(chars)) != -1){fileWriter.write(chars,0,length);
}
} catch (IOException e) {e.printStackTrace();
}finally {if(fileReader != null){try {fileReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
if(fileWriter != null){try {fileWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
六、字符緩沖流(1)BufferedReader:字符緩存輸入流。從字符輸入流讀取文本,緩沖各個字符,從而實(shí)現(xiàn)字符、數(shù)組和行的高效讀取。
FileReader:內(nèi)部使用InputStreamReader,解碼過程,byte ->char,默認(rèn)緩存大小為8K。
BufferedReader:默認(rèn)緩存大小為8K,但可以手動指定緩存大小,把數(shù)據(jù)讀取到緩存中,減少每次轉(zhuǎn)換過程,效率更高。
(2)BufferedWriter:字符緩存輸出流。將文本寫入字符輸出流,緩存各個字符,從而提供單個字符、數(shù)組和字符串的高效寫入。
FileWriter:內(nèi)部使用InputStreamWriter,解碼過程,byte ->char,默認(rèn)緩存大小為8K。
BufferedWriter:默認(rèn)緩存大小為8K,但可以手動指定緩存大小,把數(shù)據(jù)讀取到緩存中,減少每次轉(zhuǎn)換過程,效率更高。
(3)字符緩沖流與字節(jié)緩沖流的思想一樣。
public static void main(String[] args) {//寫數(shù)據(jù)
BufferedWriter bufferedWriter = null;
try {bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
bufferedWriter.write("好好學(xué)習(xí),天天向上");
} catch (IOException e) {e.printStackTrace();
}finally {if(bufferedWriter != null){try {bufferedWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
//讀數(shù)據(jù)
BufferedReader bufferedReader = null;
try {bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
char[] chars = new char[1024];
int length;
while ((length = bufferedReader.read(chars)) != -1){System.out.println(new String(chars,0,length));
}
} catch (IOException e) {e.printStackTrace();
}finally {if(bufferedReader != null){try {bufferedReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
(1)BufferedWriter
方法 | 說明 |
---|---|
void newLine() | 寫一行分隔符,行分隔符字符串由系統(tǒng)屬性定義 |
(2)BufferedReader
方法 | 說明 |
---|---|
String readLine() | 讀一行文字。結(jié)果包含行的內(nèi)容的字符串,不包括任何行終止字符,如果流的結(jié)尾已經(jīng)到達(dá),則返回null |
eg:
public static void main(String[] args) {//寫數(shù)據(jù)
BufferedWriter bufferedWriter = null;
try {bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
for(int i=1; i<=10; i++){bufferedWriter.write("qizekj"+i);
bufferedWriter.newLine();
}
} catch (IOException e) {e.printStackTrace();
} finally {if (bufferedWriter != null) {try {bufferedWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
//讀數(shù)據(jù)
BufferedReader bufferedReader = null;
try {bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
String str;
while ((str = bufferedReader.readLine()) != null) {System.out.println(str);
}
} catch (IOException e) {e.printStackTrace();
} finally {if (bufferedReader != null) {try {bufferedReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
eg:
public static void main(String[] args) {BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt"));
String str;
while ((str = bufferedReader.readLine()) != null) {bufferedWriter.write(str);
bufferedWriter.newLine();
}
} catch (IOException e) {e.printStackTrace();
} finally {if (bufferedReader != null) {try {bufferedReader.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (bufferedWriter != null) {try {bufferedWriter.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧