文件數據IO操作
【Reader和Writer】
1、字元流原理
Reader是字元輸入流的父類。
Writer是字元輸出流的父類。
字元流是以字元(char)為單位讀寫數據的。一次處理一個unicode。
字元流的底層仍然是基本的位元組流。
2、Reader與Writer常用方法
Reader的常用方法:
-int read():讀取一個字元,返回的int值「低16」位有效。
-int read(char[] chs):從該流中讀取一個位元組數組的length個字元並存入該數組,返回值為實際讀取到的字元量。
Writer的常用方法:
-void write(int c):寫出一個字元,寫出給定int值「低16」位表示的字元。
-void write(char[] chs):將給定字元數組中所有字元寫出。
-void write(String str):將給定的字元串寫出。
-void write(char[] chs,int offset,int len):將給定的字元數組中從offset開始連續的len個字元寫出。
【轉換流】
1、字元轉換流原理
InputStreamReader:字元輸入流。
-使用該流可以設置字符集,並按照指定的字符集從流中按照該編碼將位元組數據轉換為字元並讀取。
OutputStreamWriter:字元輸出流。
-使用該流可以設置字符集,並按照指定的字符集將字元轉換為對應位元組後通過該流寫出。
2、使用轉化流指定字符集編碼
InputStreamReader的構造方法允許我們設置字符集:
-InputStreamReader(InputStream in,String charsetName):基於給定的位元組輸入流以及字元編碼創建ISR
-InputStreamReader(InputStream in):該構造方法會根據系統默認字符集創建ISR
OutputStreamWriter的構造方法:
-OutputStreamWriter(OutputStream out,String charsetName):基於給定的位元組輸出流以及字元編碼創建OSW
-OutputStreamWriter(OutputStream out):該構造方法會根據系統默認字符集創建OSW
3、使用OSW輸出字元數據
public void testOutput() throws IOException{
FileOutputStream fos=new FileOutputStream("demo.txt");
OutputStreamWriter writer=new OutputStreamWriter(fos,"UTF-8");
String str="大家好!";
writer.write(str);
writer.close();
}
public void testOSW() throws Exception{
FileOutputStream fos=new FileOutputStream("osw.txt");
//根據指定編碼寫出字元串,編碼名稱忽略大小寫
OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");
osw.write("才是真的好!");
osw.close();
}
4、使用ISR輸入字元數據
public void testInput() throws IOException{
FileInputStream fis=new FileInputStream("demo.txt");
InputStreamReader read=new InputStreamReader(fis,"UTF-8");
int c=-1;
while((c=reader.read())!=-1){
}
reader.close();
}
public void testISR() throws Exception{
FileInputStream fis=new FileInputStream("osw.txt");
//根據指定編碼寫出字元串,編碼名稱忽略大小寫
InputStreamReader isr=new InputStreamReader(fis,"GBK");
int chs=-1;
while((chs=isr.read())!=-1){
}
isr.close();
}
【演示】
1、以下代碼是字元輸出流
public class OSWDemo{
public static void main(String[] args) throws IOException{
FileOutputStream fos=new FileOutputStream("osw.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");
osw.write("和我在成都的街頭走一走,哦哦哦哦。");
osw.write("直到所有的燈都熄滅了,也不停留。");
osw.close();
}
}
2、以下代碼是字元輸入流
public class ISRDemo{
public static void main(String[] args){
FileInputStream fis=new FileInputStream("osw.txt");
InputStreamReader isr=new InputStreamReader(fis);
int d=-1;
while((d=isr.read())!=-1){
}
isr.close();
}
}
3、以下是具有自動行刷新的緩衝字元輸出流
public class PWDemo{
public static void main(String[] args){
PrintWriter pw=new PrintWriter("pw.txt","UTF-8");
pw.println("該配合你演出的我視而不見。");
pw.println("別逼一個最愛你的人即興表演。");
pw.close();
}
}
4、以下也是具有自動行刷新的緩衝字元輸出流
public class PWDemo2{
public static void main(String[] args){
FileOutputStream fos=new FileOutputStream("pw2.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
PrintWriter pw=new PrintWriter(osw);
pw.println("你好!");
pw.println("呵呵!");
pw.println("嘿嘿!");
}
}
5、以下是緩衝字元輸入流
public class BRDemo{
public static void main(String[] args){
FileInputStream fis=new FileInputStream("src"+File.separator+"day09"+File.separator+"BRDemo.java");
InputStreamReader isr=new InputStreamReader(isr);
BufferedReader br=new BufferedReader(isr);
String line=null;
while((line=br.readLine())!=null){
}
br.close();
}
}
TAG:鄰家零 |