首页 > 代码库 > java io 处理流,BufferdInputStream、BufferedWriter、BufferedReader
java io 处理流,BufferdInputStream、BufferedWriter、BufferedReader
BufferdInputStream:读取helloworld。java
helloworld.java:
package io;public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); }}
package io;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class TestBufferdStream { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("f:\\share\\j2se\\IO\\HelloWorld.java"); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println(bis.read()); System.out.println(bis.read()); //在输入流中的当前位置上作标记。 //reset 方法的后续调用将此流重新定位在最后标记的位置上,以便后续读取操作重新读取相同的字节。 bis.mark(104); for(int i=0;i<10 && (c=bis.read())!=-1;i++){ System.out.print((char)c+" "); } System.out.println(); //将此流重新定位到对此输入流最后调用 mark 方法时的位置。 bis.reset(); for(int i=0;i<10 && (c=bis.read())!=-1;i++){ System.out.print((char)c+" "); } bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
BufferedWriter\BufferedReader:
例子程序:
package io;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class TestBufferdInputStream2 { public static void main(String[] args) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("f:\\share\\j2se\\IO\\dat2.txt")); BufferedReader br = new BufferedReader(new FileReader("f:\\share\\j2se\\IO\\dat2.txt")); String s = null; //写50个随机数到dat2.txt for(int i=1; i < 50; i++){ s = String.valueOf(Math.random()); bw.write(s); bw.newLine();//换行 } bw.flush(); //读取50行随机字符,一次读取一行 while((s=br.readLine()) != null){ System.out.println(s); } bw.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
dat2.txt:
0.24435601362218273
0.07126801193316767
0.445311738285074
0.35293158421910387
0.04822819969288661
0.464188774352171
0.6905170100489313
0.8547166555962266
0.5541870091691403
0.9247856173061691
0.48488882959764723
0.5193802516020373
0.04851570819954021
0.3532430846365504
0.5913492607077712
0.7269377024915321
0.46016618867662185
0.8830446251301621
0.2703242210883656
0.07017468795157189
0.10078493531688781
0.23783445875014186
0.00409907542074428
0.9501608886342727
0.3959891143119634
0.7696311492996133
0.023352826602872856
0.9771539795955477
0.9819466949500667
0.03218300631060633
0.9869752588243724
0.8710162291479447
0.003228411938660858
0.7442323830163325
0.7688111962900284
0.3218677237396862
0.44106400125074907
0.7796821087986395
0.9387643377368098
0.31955151461514586
0.825548588820152
0.38513415381788396
0.19073290448507962
0.7895878480795029
0.741009435685509
0.16999690403089585
0.6661380207280028
0.9836458017809654
0.12065762566480698
java io 处理流,BufferdInputStream、BufferedWriter、BufferedReader