Java涓殑IO妗嗘灦娴佷簩

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));

}

}

銆�

你可能感兴趣的:(Java涓殑IO妗嗘灦娴佷簩)