Bufferdinputstream: Read HelloWorld. Java
Helloworld.java:
Package io; Public class HelloWorld { publicstaticvoid main (string[] args) { System.out.println ("Hello World");} }
Packageio;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classTestbufferdstream { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("F:\\share\\j2se\\io\\helloworld.java"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intc = 0; System.out.println (Bis.read ()); System.out.println (Bis.read ()); //marks the current position in the input stream. //subsequent calls to the Reset method reposition this stream at the last marked position so that subsequent read operations reread the same bytes. Bis.mark (104); for(intI=0;i<10 && (C=bis.read ())!=-1;i++) {System.out.print (Char) c+ ""); } System.out.println (); //relocate this stream to the location when the mark method was last called on this input stream. Bis.reset (); for(intI=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:
Example program:
Packageio;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classTestBufferdInputStream2 { Public Static voidMain (string[] args) {Try{bufferedwriter bw=NewBufferedWriter (NewFileWriter ("F:\\share\\j2se\\io\\dat2.txt")); BufferedReader BR=NewBufferedReader (NewFileReader ("F:\\share\\j2se\\io\\dat2.txt")); String s=NULL; //Write 50 random numbers to Dat2.txt for(intI=1; I < 50; i++) {s=string.valueof (Math.random ()); Bw.write (s); Bw.newline ();//line Break} bw.flush (); //reads 50 random characters, one row at a time while((S=br.readline ())! =NULL) {System.out.println (s); } bw.close (); Br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.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 processing streams, Bufferdinputstream, BufferedWriter, BufferedReader