Concept:
The InputStream class is an abstract class of byte input streams and is the parent class of all byte input streams.
The OutputStream class is an abstract class of byte input streams and is the parent class of all byte output streams.
In (can be understood as read) out (can be understood as write)
One, the reading and writing of the byte stream
Both the FileInputStream class and the Fileoutstream class are used to manipulate disk files.
1.
FileInputStream (String name); Fill in a path name of type string
FileInputStream (file file); Fill in the File object
2.
Fileoutstream (String name); Fill in a path name of type string
Fileoutstream (file file); Fill in the File object
Examples are as follows:
PackageCom.inba.maya.liu;ImportJava.io.*; Public classTextsteram { Public Static voidMain (string[] args)throwsexception{//read, byte streamString S= "G:\\aaa.txt"; File F=NewFile (s); FileInputStream FS=NewFileInputStream (f); //byte[] B=new byte[10000]; in a byte stream, you can either customize the length or use the object. Length, but note that you must convert to int type byte[] b=New byte[(int) f.length ()];1 fs.read (b); Fs.close (); String Str=NewString (b); System.out.println (str); //Write, Byte stream /*//Check document String str= "66666"; String s= "G:\\bbb.txt"; File F=new file (s); FileOutputStream fos=new FileOutputStream (f); Byte[] B=str.getbytes (); Fos.write (b); Fos.close (); */ /*String str= "\r\n7777777"; String s= "G:\\bbb.txt"; FileOutputStream fos=new FileOutputStream (s,true); Byte[] B=str.getbytes (); Fos.write (b); Fos.close (); */ /** FileInputStream (f), write * FileInputStream (f,true), append * FileInputStream (s); Write * FILEINP Utstream (s,true); Additional * * FileOutputStream (s); Write * FileOutputStream (s,true); Append * FileOutputStream (f), write * FileOutputStream (f,true), append **/ }}
Second, the character stream reading and writing
Why use a character stream and what is the difference between a character stream and a byte stream:
Using the Fileoutstream class to write data to a file with the use of the Fileinstream class to read the data in the file, there is little point that these two types can only provide a byte or byte array reading, since the Chinese characters occupy two bytes in the file, if you use a byte stream, Read bad will appear garbled phenomenon, at this time need to adopt character stream FileReader and FileWriter class will avoid this phenomenon.
Method used: Read (), reading, write ();
The same filereader and FileWriter, also loaded two constructors, you can fill the path of the string type directly, you can also fill in the File object;
1.
Filereade (String fileName); Fill in a path name of type string
Filereade (file file); Fill in the File object
2.
FileWriter (String fileName); Fill in a path name of type string
FileWriter (file file); Fill in the File object
Instance:
PackageCom.inba.maya.liu;ImportJava.io.*;ImportJava.util.*; Public classTextzifu { Public Static voidMain (string[] args)throwsexception{//read, the character stream cannot be used. LengthString S= "G:\\aaa.txt"; File F=NewFile (s); FileReader FR=NewFileReader (f); Char[] c=New Char[5000]; Fr.read (c); Fr.close (); String Str=NewString (c); System.out.println (str); //Write character Stream /*String s= "G:\\qqq.txt"; FileWriter fw=new FileWriter (s,true); Fw.write ("\r\n9999999"); Fw.close (); */ }}
Iii. input and output streams with cache
Caching can be said to be a performance optimization for I/O, and the cache stream adds memory buffers to I/O streams. There is a cache to go, which makes it possible to execute skip () on the stream, mark (), Reset ();
The Bufferedinputstream class (read cache stream) can be packed with buffers for any of the InputStream classes to optimize performance. (so it is generally used to package the word stream!) )
The same Bufferedinputstream class and the Bufferedoutputstream class have two constructors;
1.
Bufferedinputstream (Input stream)
Bufferedinputstream (input stream, int size); Size: Create a buffer by the specified size
2.
Bufferedoutputstream (output stream)
Bufferedoutputstream (output stream, int size); Size: Create a buffer by the specified size
Instance:
PackageCom.inba.maya.liu;ImportJava.io.*;ImportJava.util.*; Public classTextzifu { Public Static voidMain (string[] args)throwsexception{//read, Cache stream /*String s= "G:\\aaa.txt"; File F=new file (s); FileInputStream fis=new FileInputStream (s); Bufferedinputstream bis=new bufferedinputstream (FIS); Byte[] b=new byte[(int) f.length ()]; Bis.read (b); Bos.close ();//must first close the cache stream Fos.close ();//And then close the stream string Str=new string (b); System.out.println (str); */ //Write, Cache streamString s= "G:\\qqq.txt"; FileOutputStream Fos=NewFileOutputStream (s),true); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS); String Str= "\r\n1111111"; byte[] b=str.getbytes (); Bos.write (b); Bos.flush ();//write a call to flush (); Complete the input and clear the bufferBos.close ();//Be sure to close the cache stream firstFos.close ();//and then close the flow }}
Four, read the data with scanner:
PackageCom.inba.maya.liu;ImportJava.io.*;ImportJava.util.*; Public classTextzifu { Public Static voidMain (string[] args)throwsexception{/*String s= "G:\\aaa.txt"; File F=new file (s); Scanner sc=new Scanner (f); Hasnextline: If there is another row in the input of this scanner, true while (Sc.hasnextline ()) {System.out.println (Sc.nextline ()) is returned; } sc.close (); */ //constructs a string generator with no characters, with an initial capacity of 16 characters. StringBuilder sb=NewStringBuilder (); String s= "G:\\aaa.txt"; File F=NewFile (s); Scanner SC=NewScanner (f); while(Sc.hasnextline ()) {Sb.append (Sc.nextline () )+ "\ r \ n"); } sc.close (); System.out.print (SB); }}
Character Stream, byte stream, buffered input/output stream, and read file with scanner