瀛楃缂撳啿娴丅ufferedWriter鍜孊ufferedWriter
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了瀛楃缂撳啿娴丅ufferedWriter鍜孊ufferedWriter相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/rsh' title='rsh'>rsh
exce stat 鎹㈣ 鏂囦欢 楂樻晥 杈撳叆 鏂瑰紡 cep瀛楃缂撳啿娴?/strong>
1銆佸熀鏈敤娉?/strong>
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 /** 8 * BufferedWriter:灏嗘枃鏈啓鍏ュ瓧绗﹁緭鍑烘祦锛岀紦鍐插悇涓瓧绗︼紝浠庤€屾彁渚涘崟涓瓧绗︺€佹暟缁勫拰瀛楃涓茬殑楂樻晥鍐欏叆銆? 9 * BufferedWriter:浠庡瓧绗﹁緭鍏ユ祦涓鍙栨枃鏈紝缂撳啿鍚勪釜瀛楃锛屼粠鑰屽疄鐜板瓧绗︺€佹暟缁勫拰琛岀殑楂樻晥璇诲彇銆?10 * @author Administrator 11 * 12 */ 13 public class BufferedStreamDemo { 14 public static void main(String[] args) throws IOException { 15 //鍒涘缓缂撳啿娴佸璞?/span> 16 BufferedWriter bw=new BufferedWriter(new FileWriter("C:\Test\bw.txt")); 17 bw.write("hello"); 18 //bw.flush(); 19 bw.close(); 20 21 //涓€娆¤鍙栦竴涓瓧绗?22 //鍒涘缓杈撳叆缂撳啿娴佸璞?/span> 23 BufferedReader br=new BufferedReader(new FileReader("C:\Test\bw.txt")); 24 int ch; 25 while((ch=br.read())!=-1) { 26 System.out.print((char)ch); 27 } 28 br.close(); 29 System.out.println(); 30 //涓€娆¤鍙栦竴涓瓧绗︽暟缁?/span> 31 BufferedReader br2=new BufferedReader(new FileReader("C:\Test\bw.txt")); 32 int len=0; 33 char chs[]=new char[1024]; 34 while((len=br2.read(chs))!=-1) { 35 System.out.print(new String(chs,0,len)); 36 } 37 br2.close(); 38 } 39 }
2銆佺紦鍐叉祦澶嶅埗鏂囨湰鏂囦欢涓ょ鏂瑰紡
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 /** 8 * 闇€姹傦細鎶婇」鐩矾寰勪笅鐨凚ufferedStreamCopy.java涓殑鍐呭澶嶅埗鍒伴」鐩矾寰勪笅鐨刢opy.java涓? 9 * @author Administrator 10 * 11 */ 12 public class BufferedStreamCopy { 13 public static void main(String[] args) throws IOException { 14 BufferedReader br=new BufferedReader(new FileReader("BufferedStreamCopy.java")); 15 BufferedWriter bw=new BufferedWriter(new FileWriter("copy.java")); 16 //绗竴绉嶆柟寮忥紝涓€娆¤鍙栦竴涓瓧绗?17 // int ch; 18 // while((ch=br.read())!=-1) { 19 // bw.write(ch); 20 // } 21 22 //绗簩绉嶆柟寮忥紝涓€娆¤鍙栦竴涓瓧绗︽暟缁?/span> 23 int len; 24 char[] chs=new char[1024]; 25 while((len=br.read(chs))!=-1) { 26 bw.write(chs,0,len); 27 } 28 29 br.close(); 30 bw.close(); 31 32 } 33 }
3銆佺紦鍐叉祦鐨勭壒娈婂姛鑳?/span>
BufferedWriter:
銆€銆€銆€銆€void newLine():鍐欎竴涓崲琛岀锛岃繖涓崲琛岀鐢辩郴缁熷喅瀹?/p>
BufferedReader
銆€銆€銆€銆€String readLine():涓€娆¤鍙栦竴琛屾暟鎹紝浣嗘槸涓嶈鍙栨崲琛岀
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 /** 8 * 闇€姹傦細鎶婇」鐩矾寰勪笅鐨凚ufferedStreamCopy.java涓殑鍐呭澶嶅埗鍒伴」鐩矾寰勪笅鐨刢opy.java涓? 9 * @author Administrator 10 * 11 */ 12 public class BufferedStreamCopy { 13 public static void main(String[] args) throws IOException { 14 BufferedReader br=new BufferedReader(new FileReader("copy1.java")); 15 BufferedWriter bw=new BufferedWriter(new FileWriter("copy2.java")); 16 String str; 17 while((str=br.readLine())!=null) { 18 bw.write(str); 19 bw.newLine(); 20 } 21 br.close(); 22 bw.close(); 23 24 BufferedReader brshow=new BufferedReader(new FileReader("copy2.java")); 25 String str2; 26 while((str2=brshow.readLine())!=null) { 27 System.out.println(str2); 28 } 29 brshow.close(); 30 31 } 32 }
以上是关于瀛楃缂撳啿娴丅ufferedWriter鍜孊ufferedWriter的主要内容,如果未能解决你的问题,请参考以下文章