1聽 聽 聽 IO(浜�)No19
銆�
缂撳啿娴侊細鍐呯疆浜嗙紦鍐插尯锛屽鐜版湁鐨勬祦瀵硅薄杩涜浜嗗皝瑁咃紝瀹炵幇浜嗛珮鏁堢殑璇诲啓鎿嶄綔骞跺寮轰簡鍔熻兘
瀛楄妭缂撳啿娴�
BufferedInputStream
BufferedOutputStream
瀛楃缂撳啿娴�
BufferedReader
鐗规湁鏂规硶锛歳eadLine():绗竴琛岋紙浠ユ崲琛岀涓烘爣璁帮紝璇诲埌鎹㈣绗︿唬琛ㄦ槸涓�琛岋紝涓嶅寘鍚崲琛岀锛�
BufferedWriter
鐗规湁鏂规硶锛歯ewLine():杈撳嚭涓�涓崲琛岀(鏍规嵁鏈湴绯荤粺鐩稿叧)
璁捐妯″紡锛�
銆愮畝鍗曞伐鍘傘�侀潤鎬佸伐鍘傘�佹娊璞″伐鍘傘�佸崟鍒╂ā寮忋�佽凯浠e櫒妯″紡銆佽瀵熻�呮ā寮�(鎺ュ彛鍥炶皟)銆�
瑁呴グ鑰呰璁℃ā寮忥細
銆�
1.1聽聽聽聽澶勭悊娴�
鏄ㄥぉ鐨凢ileStream绯诲垪銆丗ileReader銆乄riter閮芥槸鑺傜偣娴侊紝鐩存帴鍜屾枃浠惰繖涓妭鐐规墦浜ら亾鐨勩�傝妭鐐规祦瀵瑰簲鐨勬槸澶勭悊娴侊紝瀹冧滑涓嶇洿鎺ュ拰鏂囦欢銆佺綉缁滅瓑缁堢鑺傜偣鎵撲氦閬擄紝鑰屾槸瀵瑰叾浠栨祦杩涜瑁呴グ銆佺粰鍏朵粬娴佹坊鍔犲姛鑳�
澶勭悊娴佹湰韬篃鏄疘nputStream銆丱utputStream銆丷eader銆乄riter鐨勫瓙绫伙紝濡傦細
BufferedInputStream銆丅ufferedOutputStream銆丅ufferedReader銆丅ufferedWriter锛屽畠浠彲浠ョ粰瀵瑰簲鐨勪綋绯讳腑鐨勬祦杩涜瑁呴グ锛屾坊鍔犵紦鍐插姛鑳�
1.2聽聽聽聽BufferedInputStream
瀛楄妭杈撳叆缂撳啿娴侊細
銆�
FileInputStream fis = newFileInputStream("g:/output.txt");
//鍒涘缓涓�涓紦鍐插瓧鑺傝緭鍏ユ祦锛岃楗颁簡鍙傛暟涓殑fis鏂囦欢杈撳叆娴侊紝缁欏畠娣诲姞浜嗙紦鍐插姛鑳�
BufferedInputStream聽 bis = new BufferedInputStream(fis);
//璇讳竴涓瓧鑺傦紝浣嗕笉鏄洿鎺ユ搷浣滅鐩樿锛岃�屾槸浠庡唴缃殑缂撳瓨涓幓璇�
intn = bis.read();
System.out.println(n);
bis.close();聽聽聽聽聽 //鍏抽棴bis浼氳嚜鍔ㄥ叧闂瑁呴グ鐫�鐨勬祦
BufferedInputStream bis = new BufferedInputStream(newFileInputStream(
"g:/output.zip"));
intn =0;
while((n=bis.read())!=-1){
//涓嶄綔浠讳綍澶勭悊锛岀函绮逛负婕旂ず鏁堢巼闂
//鏁堢巼楂橈紝鍥犱负涓嶆槸鐩存帴浠庣鐩樹笂涓�涓釜璇伙紝鑰屾槸浠庣紦鍐插尯涓竴涓釜璇�
}
bis.close();//鍏抽棴缂撳瓨娴侊紝閲岄潰鎵�瑁呴グ鐫�鐨勬祦涔熶細鍏抽棴
銆�
BufferedInputStream 甯︾紦鍐茬殑瀛楄妭杈撳叆娴�
鏋勯�犳柟娉�:BufferedInputStream(InputStream is)鍙傛暟鏄杩涜瑁呴グ鐨勫彟涓�涓瓧鑺傝緭鍏ユ祦
鏂规硶鍜孖nputStream鍩烘湰涓�鑷�
int read()浠庣紦鍐蹭腑璇讳竴涓瓧鑺�(缂撳啿绌轰簡璇濆氨璇讳俊鎭~鍏�)
close()鍏抽棴澶勭悊娴佹椂锛屼細鑷姩鍏抽棴琚楗扮殑娴�
1.3聽聽聽聽BufferedOutputStream
瀛楄妭杈撳嚭缂撳啿娴侊細
銆�
//鐢ㄧ紦瀛樺瓧鑺傝緭鍑烘祦瑁呴グ鏂囦欢杈撳嚭娴侊紝娣诲姞缂撳瓨鍔熻兘
BufferedOutputStreambos = new BufferedOutputStream(
newFileOutputStream("g:/output.txt"));
bos.write(97);//灏嗘暟鎹啓鍒板唴缃紦瀛樹腑锛岃繖閲屾病鏈夊疄闄呭啓鍒版枃浠朵腑
bos.flush();聽 //寮哄埗鍒风紦瀛�,鍒峰埌鏂囦欢涓�
//鍏抽棴缂撳瓨娴侊紝浼氬厛鍒风紦瀛�(97杩欐椂鍊欐墠鍐欏埌鏂囦欢閲�)锛屾墍瑁呴グ鐨勬祦涔熶細琚嚜鍔ㄥ叧闂�
bos.close();
BufferedInputStream bis = new BufferedInputStream(newFileInputStream(
"g:/output.zip"));
BufferedOutputStreambos = new BufferedOutputStream(
newFileOutputStream("g:/AAoutput.zip"));
intn =0;
while((n=bis.read())!=-1){
bos.write(n);聽聽聽 //鍐欏埌鐩爣鏂囦欢鐨勭紦瀛樿緭鍑烘祦涓幓
//聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 bos.flush();聽聽聽聽 //姣忔閮界洿鎺ユ搷浣滅鐩橈紝鏁堢巼鏋佷綆
}
//聽聽聽聽聽聽聽聽聽聽聽聽聽聽 bos.flush();聽聽聽聽聽聽聽聽 //鎵嬪姩flush
bos.close();聽聽聽聽聽聽聽聽 //鑷姩flush
bis.close();
銆�
BufferedOutputStream 甯︾紦鍐茬殑瀛楄妭杈撳嚭娴�
鏋勯�犳柟娉�:BufferedOutputStream(OutputStream os)鍙傛暟鏄杩涜瑁呴グ鐨勫彟涓�涓瓧鑺傝緭鍑烘祦
鏂规硶鍜孫utputStream鍩烘湰涓�鑷�
write(int b)鍚戠紦鍐蹭腑鍐欎竴涓瓧鑺�(缂撳啿婊′簡璇濆氨灏嗙紦鍐蹭腑鐨勬墍鏈夊瓧鑺傛暟鎹竴涓嬪瓙鍒峰埌琚楗扮殑娴佷腑)
flush()鍒风紦瀛橈紝寮哄埗灏嗙紦瀛樹腑鐨勫瓧鑺傛暟鎹埛鍒拌瑁呴グ寰楁祦涓�
close()鍦ㄥ叧闂箣鍓嶄細鍏堥殣寮忚皟鐢╢lush锛岀劧鍚庡啀鍏抽棴
鍏抽棴澶勭悊娴佹椂锛屼細鑷姩鍏抽棴琚楗扮殑娴�
1.4聽聽聽聽BufferedReader
瀛楃缂撳啿娴�:
銆�
BufferedReader br =new BufferedReader(new FileReader("g:/output.txt"));
//浠庣紦瀛樹腑璇诲彇涓�涓瓧绗﹀苟鎵撳嵃
System.out.println((char)br.read());
br.close();
BufferedReader br =new BufferedReader(new FileReader("g:/output.txt"));
String line聽 = br.readLine();聽聽聽 //璇诲彇涓�琛屼俊鎭紝鐢╨ine鎸囧悜
System.out.println(line);
//璇讳笅涓�琛岋紝鎵撳嵃锛屼篃灏辨槸绗簩琛�
System.out.println(br.readLine());
//璇讳笅涓�琛岋紝鎵撳嵃锛屼篃灏辨槸绗笁琛�
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
//璇诲畬杩斿洖null;
System.out.println(br.readLine());
br.close();聽聽 //鍏抽棴澶勭悊娴佸強鍏惰楗扮殑娴�
BufferedReader br =new BufferedReader(new FileReader("g:/output.txt"));
String line = null; //瀛樺偍姣忔readLine鐨勭粨鏋滃強鍒ゆ柇鏂囦欢灏鹃儴
while (true) {
line =br.readLine();//寰幆璇昏骞惰祴缁檒ine
if (line == null){//濡傛灉鍒拌揪鏂囦欢灏鹃儴
break; //閫�鍑哄惊鐜�
}
System.out.println(line);//鎵撳嵃
}
br.close();//鍏抽棴澶勭悊娴佸拰鑺傜偣娴�
//鍒朵綔涓�涓嚜鍔ㄦ壒鏀瑰櫒锛屼竴棰樹竴鍒嗭紝鏈�缁堟墦鍗板嚭璇曞嵎鐨勬垚缁�
BufferedReaderreader1 = new BufferedReader(new FileReader("g:/answer.txt"));
BufferedReader reader2 = newBufferedReader(new FileReader("g:/tom.txt"));
int count = 0;
while(true){
String line1 =reader1.readLine();
String line2 =reader2.readLine();
if(line1 == null ||line2 ==null){
break;
}
if(line1.trim().equalsIgnoreCase(line2.trim())){
count ++;
}
}
System.out.println(count);
reader1.close();
reader2.close();
銆�
BufferedReader 甯︾紦鍐茬殑瀛楃杈撳叆娴�
鏋勯�犳柟娉�:BufferReader(Reader r)鍙傛暟鏄杩涜瑁呴グ鐨勫彟涓�涓瓧绗﹁緭鍏ユ祦
int read()浠庣紦瀛樹腑璇讳竴涓瓧绗︼紝璇诲畬鐨勮瘽杩斿洖-1锛屾病璇诲畬灏辫繑鍥炶鍒扮殑閭d釜瀛楃
String readLine()浠庣紦瀛樹腑璇讳竴琛岋紝濡傛灉娌℃湁璇诲畬锛岃繑鍥炶〃绀虹殑涓嬩竴琛岀殑瀛楃涓诧紝濡傛灉璇诲畬锛岃繑鍥瀗ull
1.5聽聽聽聽BufferedWriter
BufferedWriter 甯︾紦鍐茬殑瀛楃杈撳嚭娴�
鏋勯�犳柟娉�:BufferedWriter(Writer w)鍙傛暟鏄杩涜瑁呴グ鐨勫彟涓�涓瓧绗﹁緭鍑烘祦
write(int c)寰�缂撳瓨涓啓涓�涓瓧绗�(缂撳啿婊′簡浼氳嚜鍔ㄥ埛鍑哄幓)
write(String s)灏嗗弬鏁板瓧绗︿覆鎷嗘垚涓�缁勫瓧绗﹀啓鍒扮紦瀛樹腑鍘�
newLine()寰�缂撳瓨涓啓鎹㈣绗�(windows涓崲琛孿r\n,linux\n)
flush()鎶婄紦瀛樹腑鐨勫瓧鑺備俊鎭竴涓嬪瓙寮哄埗鍒峰埌鎵�瑁呴グ鐨勬祦涓�
close()鍏抽棴缂撳啿娴佸強鍏惰楗扮殑娴侊紝骞堕殣寮廸lush()
1.6聽聽聽聽杞崲娴�
銆�
鍦⊿ystem绫讳腑瀹氫箟浜嗕笁涓潤鎬佸瓧娈碉紝鍒嗗埆鏄笁涓祦瀵硅薄
1锛塖ystem.err鈥滄爣鍑嗏�濋敊璇緭鍑烘祦锛屽彲灏嗛敊璇暟鎹緭鍑哄埌灞忓箷锛堟帶鍒跺彴锛�
2锛塖ystem.out鈥滄爣鍑嗏�濊緭鍑烘祦锛屽彲灏嗘暟鎹緭鍑哄埌灞忓箷锛堟帶鍒跺彴锛�
3锛塖ystem.in鈥滄爣鍑嗏�濊緭鍏ユ祦锛屽彲鑾峰彇浠庨敭鐩樺綍鍏ョ殑鏁版嵁
杞崲娴�
浠庨敭鐩樺綍鍏ユ暟鎹墍鐢ㄧ殑娴佹槸涓�涓郴缁熸彁渚涚殑鏍囧噯娴侊紝鑰岀郴缁熸彁渚涚殑鏍囧噯璺兘鏄瓧鑺傛祦
濡傛灉鐩存帴浣跨敤瀛楄妭娴佹潵鎿嶄綔瀛楃鏁版嵁鐨勮瘽锛屼細鍑虹幇寰堝闂锛岄渶瑕佸仛寰堝棰濆鐨勬搷浣�
鎴戜滑鍙互閫氳繃杞崲娴佸皢瀛楄妭娴佽浆鎹㈡垚瀛楃娴侊紝JDK涓竴鍏辨彁渚涗簡涓や釜杞崲娴�
InputStreamReader锛氭槸瀛楄妭娴侀�氬悜瀛楃娴佺殑妗ユ
OutputStreamWriter锛�鏄瓧绗︽祦閫氬悜瀛楄妭娴佺殑妗ユ
瀹為檯涓婏細瀛楃娴�=瀛楄妭娴�+缂栫爜琛�
//绯荤粺鎻愪緵涓変釜鏍囧噯娴佷笉瑕佹墜鍔ㄥ叧闂紝涓�鏃﹀叧闂氨浣跨敤涓嶆潵浜嗕簡銆�
銆�
1.6.1聽 InputStreamReader
杞崲娴侊細灏嗗瓧鑺傛祦杞崲鎴愬瓧绗︽祦锛屼娇鍏惰兘澶熻緭鍏ユ眽瀛楋細
銆�
FileInputStream fis = newFileInputStream("g:/output.txt");
//浣跨敤杞崲娴佸鍏惰鎹�
InputStreamReaderisr = new InputStreamReader(fis);
intn=0;
while((n=isr.read())!=-1){
System.out.println((char)n);
}
isr.close();
鐢ㄨ浆鎹㈡祦鎸囧畾瀛楃闆嗭細
//聽聽聽聽聽聽聽聽聽聽聽聽聽聽 FileReaderisr = new FileReader("g:/answer.txt");
FileInputStreamfis = new FileInputStream("g:/answer.txt");
//瑁呮崲涓哄瓧绗︽祦
InputStreamReaderisr = new InputStreamReader(fis,"GBK");
//鐢ㄧ紦鍐叉祦瑁呴グ瑁呮崲鍑烘潵鐨勫瓧绗︽祦锛屾坊鍔犱簡缂撳啿鐨勫姛鑳�
BufferedReaderbr = new BufferedReader(isr);
Stringline = br.readLine();聽聽聽 //璇昏
System.out.println(line);聽聽聽聽聽聽 //鎵撳嵃
br.close();
FileOutputStream fos = newFileOutputStream("g:/answer.txt");
Stringstr = "瀹夊崜";
byte[]bytes = str.getBytes("utf-8");
for(byte b : bytes) {
System.out.println(b);
fos.write(b);//灏嗕袱涓眽瀛楀搴旂殑4涓瓧鑺傚啓鍒版枃浠朵腑鍘�
}
fos.close();
FileInputStream fis = newFileInputStream("g:/output.txt");
//鐢╱tf-8鐨勫舰寮忓皢瀛楄妭杞崲涓哄瓧绗�
InputStreamReaderisr = new InputStreamReader(fis,"utf-8");
System.out.print((char)isr.read());
System.out.print((char)isr.read());
isr.close();
//鏈夋椂鍊欐垜浠幏鍙栧埌鐨勮緭鍏ユ祦灏辨槸瀛楄妭娴侊紝鏃犳硶鏀瑰彉锛屽:System.in
//杩欐椂锛屽彲浠ヤ娇鐢↖nputStreamReader杞崲涓哄瓧绗︽祦
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
Stringline = null;
//浠庣郴缁熻緭鍏ヨ鍙栦竴琛�
while((line= br.readLine())!=null){
if(line.equals("exit")){
break;
}
System.out.println(line);
}
/***
*
*鑾峰彇鐢ㄦ埛閿洏褰曞叆鐨勫皬鍐欐暟鎹紝骞跺皢鏁版嵁鍙樻垚澶у啓鏄剧ず鍦ㄦ帶鍒跺彴涓婏紝
*濡傛灉鐢ㄦ埛杈撳叆鐨勬槸exit,鍒欑粨鏉熼敭鐩樺綍鍏�
*
*/
public class TransferDemo {
/*
*浠庨敭鐩樺綍鍏ユ暟鎹墍鐢ㄧ殑娴佹槸涓�涓郴缁熸彁渚涚殑鏍囧噯娴侊紝鑰岀郴缁熸彁渚涚殑鏍囧噯璺兘鏄瓧鑺傛祦
*濡傛灉鐩存帴浣跨敤瀛楄妭娴佹潵鎿嶄綔瀛楃鏁版嵁鐨勮瘽锛屼細鍑虹幇寰堝闂锛岄渶瑕佸仛寰堝棰濆鐨勬搷浣�
*鎴戜滑鍙互閫氳繃杞崲娴佸皢瀛楄妭娴佽浆鎹㈡垚瀛楃娴侊紝JDK涓竴鍏辨彁渚涗簡涓や釜杞崲娴�
* InputStreamReader锛氭槸瀛楄妭娴侀�氬悜瀛楃娴佺殑妗ユ
* OutputStreamWriter锛氭槸瀛楃娴侀�氬悜瀛楄妭娴佺殑妗ユ
*/
publicstatic void main(String[] args) throws IOException {
//InputStreamReader:鏄瓧鑺傛祦閫氬悜瀛楃娴佺殑妗ユ
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
//OutputStreamWriter:鏄瓧绗︽祦閫氬悜瀛楄妭娴佺殑妗ユ
BufferedWriterbw = new BufferedWriter(new OutputStreamWriter(System.out));
Stringline = null;
//浠庨敭鐩樺綍鍏ヤ竴琛屾暟鎹紝浠ユ崲琛岀涓烘爣璁�
while((line= br.readLine()) != null){
if("exit".equalsIgnoreCase(line)){
break;
}
bw.write(line.toUpperCase());
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
銆�
杞崲娴佸彲浠ョ敤鏉ュ皢瀛楄妭娴佽浆鎹负瀛楃娴�
InputStreamReader灏嗗瓧鑺傝緭鍏ユ祦杞崲涓哄瓧绗﹁緭鍏ユ祦
鏋勯�犳柟娉�:InputStreamReader(InputStream is)鍙傛暟灏辨槸瑕佽浆鎹负瀛楃娴佺殑瀛楄妭娴侊紝鏋勯�犳柟娉曡皟鐢ㄥ悗杞崲鍗虫垚鍔燂紝鎺ヤ笅鏉ュ氨鍙互瀵规瀯閫犲嚭鏉ョ殑瀛楃娴佽繘琛屾搷浣�
(鏈夊彟涓�涓噸杞芥瀯閫犳柟娉曪紝鍙互鍦╪ew鐨勬椂鍊欐寚瀹氬瓧绗﹂泦)
1.6.2聽 OutputStreamWriter
銆�
//宸茬粡鏈変竴涓瓧鑺傝緭鍑烘祦锛屾兂寰�閲屽啓utf-8鏍煎紡鐨勪腑鏂�
FileOutputStream fos= new FileOutputStream("g:/answer.txt");
//鐢ㄨ浆鎹㈡祦瑁呴グ鑺傜偣娴侊紝寰楀埌涓�涓瓧绗︽祦锛岃浆鎹㈣繃绋嬩腑浣跨敤utf-8缂栫爜鏍煎紡
OutputStreamWriter osw = newOutputStreamWriter(fos,"utf-8");
osw.write('寰�');//杩欎釜瀛楃浼氬厛浠tf-8鏍煎紡杞崲涓�3涓瓧鑺傦紝鍐嶅啓鍒板簳灞傜殑娴佷腑
osw.write('绋�');
osw.close();
銆�
OutputStreamWriter灏嗗瓧鑺傛暟鍑烘祦杞崲涓哄瓧绗﹁緭鍑烘祦
鏋勯�犳柟娉�:OutputStreamWriter(OutputStream os)鍙傛暟灏辨槸瑕佽浆鎹负瀛楃娴佺殑瀛楄妭娴�
(鍙互鍦ㄥ弻鍙傞噸杞芥瀯閫犳柟娉曚腑鎸囧畾瀛楃闆�)
瀹炰緥锛�
銆�
public class Demo3 {
publicstatic void main(String[] args) throws IOException {
//聽聽聽聽聽聽聽聽聽聽聽聽聽聽 test1();
//鎸夌収鎸囧畾鐨勫瓧绗﹀啓鏁版嵁
BufferedWriterbw = new BufferedWriter(new OutputStreamWriter(
newFileOutputStream("trans.txt"), "UTF-8"));
bw.write("閽变粖鍑jdjb12521");
bw.newLine();
bw.write("灏忓己......");
bw.close();
BufferedReaderbr = new BufferedReader(new InputStreamReader(
newFileInputStream("trans.txt"), "UTF-8"));
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
}
privatestatic void test1() throws IOException {
/*
* FileReader鏄疘nputStreamReader鐨勫瓙绫伙紝鏄竴涓究鎹风被 杩欎釜绫讳娇鐢ㄤ簡榛樿鐨勭紪鐮佸拰瀛楄妭缂撳啿鍖�
*/
/*
* 濡傛灉缂栫爜涓嶇粺涓�浼氬嚭鐜颁贡鐮佹儏鍐� FileReader fr = newFileReader("e:\\testabc.txt");
* char[] cbuf = new char[1024]; int len =fr.read(cbuf);
* System.out.println(new String(cbuf, 0,len));
*/
//浣跨敤鎸囧畾鐨勭紪鐮佹潵璇诲彇鎸囧畾鏂囨湰鏂囦欢涓殑鏁版嵁
FileInputStreamfis = new FileInputStream("E:\\testabc.txt");
//灏嗗瓧鑺傛祦杞崲涓哄瓧绗︽祦 瀹為檯涓婏細瀛楃娴� = 瀛楄妭娴� + 瀛楃缂栫爜
InputStreamReaderosr = new InputStreamReader(fis, "UTF-8");
char[]cbuf = new char[1024];
intlen = osr.read(cbuf);
System.out.println(newString(cbuf, 0, len));
}
}
銆�