The Java IO streams

  Then on one of the "Java's File class," the essay, on the basis of the File class, we went into the Java IO streams it.

 

The concept and role stream

  Flow is a set of sequential, starting and ending with a collection of bytes, it is the general term for data transmission or abstract. That data transmission between two devices is called flow, the nature of the stream data transmission, data transmission characteristics of the various types of flow abstract, convenient data manipulation more intuitive.

 

Stream classification:

  (1), according to different data flows are divided into: an input stream (read from the disk, network, etc. to the memory can only be read, not written), the output stream (written from memory to disk, the network can write, can not be read)

  (2), in accordance with the processing unit data divided into: byte stream (in bytes basic operation unit), the character stream (a basic unit of operation in characters)

  (3), divided according to different roles: Node stream (IO write to a device / node (file disk, network, etc.) directly to the data, also referred to as lower stream), the process flow (for packaging an existing stream, read and write operations to the existing flow through, does not directly operate IO node, also known as advanced stream, flow packing)

 

Java stream class structure:

classification

Input stream of bytes

Byte output stream

Character-input stream

Character-output stream

Abstract base class

InputStream

OutputStream

Reader

Writer

Action File

FileInputStream

FileOutputStream

FileReader

FileWriter

Operation array

ByteArrayInputStream

ByteArrayOutputStream

CharArrayReader

CharArrayWriter

String operations

   

StringReader

StringWriter

Buffer flow

BufferedInputStream

BufferedOutputStream

BufferedReader

BufferedWriter

Conversion flow

   

InputStreamReader

OutputStreamWriter

Object stream (for serialization)

ObjectInputStream

ObjectOutputStream

   

Abstract base class (for filtering)

FilterInputStream

FilterOutputStream

FilterReader

FilterWriter

Print stream (output function is extremely large)

 

PrintStream

 

PrintWriter

 

Use IO stream of java

InputStream input stream of bytes is the top-level parent class, method used:

  • int read () // byte read returns the data byte Unicode code value
  • int read (byte [] buff) // read up buff.length bytes, the read data in the array buff, returns the number of bytes actually read
  • int read (byte [] buff, int off, int length) // read bytes up to length, buff on the array, starting at the off position data from the array and returns the number of bytes actually read. generally set off to 0, length is generally set to the length of the buff.
        // 1, create a File class object 
        File File = new new ( "hello.txt" File );
         // 2, create an object of class FileInputStream 
        FileInputStream FIS = new new FileInputStream (File);
         // 3, calls the FileInputStream method achieve file read file 
        int B = fis.read ();
         the while (B = -1! ) { 
            of System.out.print (( char ) + B "" ); 
            B = fis.read (); 
        } 
        FIS. close ();        

 

 

 

OutputStream is the output stream of bytes top-level parent class, method used:

  • void write (int i) \\ output a byte, the value of i is a code, i.e. code value read () is obtained, the output of the corresponding byte i
  • void write (byte [] buff) // entire contents of the output byte array
  • void write (byte [] buff, int off, int length) // byte array starting from the off position, the output length of the content length bytes
        File file = new File("hello1.txt");
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(file);
            fos.write(new String("\nI Love CYT!").getBytes());
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fos != null){
                try {
                    fos.close();
                } catch(IOException e) { 
                    e.printStackTrace (); 
                } 
            } 
        }

 

 

Top Reader parent when a character input stream, commonly used methods:

  • int read () // read a character, the value of the Unicode character code, the character note not return.
  • int read (char [] buff) // read up buff.length characters, buff on the array, the number of characters actually read
  • int read (char [] buff, int off, int length) // read bytes up to length, buff on the array, starting at the off position data from the array and returns the number of characters actually read. generally set off to 0, length is generally set to the length of the buff
            File file = new File("1.txt");
            FileReader fr = null;
            try{
                fr = new FileReader(file);
                char[] c = new char[24];
                int len;
                while((len = fr.read(c)) != -1){
                    for(int i=0;i<len;i++){
                        System.out.print(c[i]);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(fr != null){
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }    

     

 

 

 

Writer is the top-level parent character output stream, commonly used methods:

  • void write (int i) // output a character, a code value i, i is the output of the corresponding character
  • void write (char [] buff) // output of the entire char [] content
  • void write (char [] buff, int off, int length) // the char [] starting from the off position, the length of the output character content length

You may be used instead of String char [], so Writer has the following two methods:

  • void  write(String str)
  • void  write(String str, int off, int length)
        Fw = FileWriter null ;
         the try { 
            fw = new new FileWriter ( new new File ( "1.txt" )); 
            String str = "I like Java, Java I want to be engineers." ; 
            Fw.write (str); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } the finally {
             IF (FW =! null ) {
                 the try { 
                    fw.close (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                }
            }
        }
    }

 

 

Stream is buffered and level 4 corresponding to the top-level parent: Buffered prefix

  InputStream BufferedInputStream byte stream input buffer, as all input stream of bytes buffered stream class

  Output buffer byte stream OutputStream BufferedOutputStream

  Reader BufferedReader character stream input buffer

  Writer BufferedWriter flow character output buffer

// use BufferedReader to read the file in, and then write out the contents BufferedWriter 
        File File = new new ( "hello.txt" File ); 
        File file1 = new new ( "hello3.txt" File ); 
        FileReader fr = null ; 
        BufferedReader br = null ; 
        FileWriter FW = null ; 
        BufferedWriter, BW = null ;
         the try { 
            fr = new new the FileReader (File); 
            FW = new new FileWriter (file1); 
            br =new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            String str;
            while((str = br.readLine()) != null){
//                System.out.println(str);
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

Otherwise I will not explained. Interested students can look deeper into themselves.

Finally, I will provide a common method of file copy it. Write your own generic class. Find it easier to understand the use of Java IO streams.

Package Penalty for IO;
 / * 
 * achieve copy files 
 * / 
Import java.io.BufferedInputStream;
 Import java.io.BufferedOutputStream;
 Import java.io.File;
 Import java.io.FileInputStream;
 Import java.io.FileOutputStream;
 Import the Java. io.FileReader;
 Import java.io.FileWriter;
 Import java.io.IOException; 

public  class the CopyFile { 

    // copy method of bytes of the file 
    public  static  void copyFileByte (the src String, String dest) {
         // . 1, to provide read, write files 
        file file1 = new newFile (the src); 
        File file2 = new new File (dest);
         // 2, to provide a respective stream of 
        the FileInputStream FIS = null ; 
        a FileOutputStream fos = null ;
         the try { 
            FIS = new new the FileInputStream (file1); 
            fos = new new a FileOutputStream (file2);
             // 3, file copy 
            byte [] B = new new  byte [20 is ];
             int len;
             the while (! (len = fis.read (B)) = -1 ) {
                fos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
        } 
    } 

    / * 
     * use FileReader, FileWriter copied text file can be realized 
     * For non-text files (video files, audio files, images), only the byte stream copy. 
     * / 
    Public  static  void copyFileChar (the src String, String dest) {
         // . 1, to provide read and write files 
        File file1 = new new File (the src); 
        File file2 = new new File (dest);
         // 2, provide the corresponding flowing 
        the FileReader fr = null ; 
        FileWriter FW = null;
        try {
            fr = new FileReader(file1);
            fw = new FileWriter(file2);
            // 3、实现文件的复制
            char[] c = new char[24];
            int len;
            while ((len = fr.read(c)) != -1) {
                fw.write(c, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null ) {
                 the try { 
                    fw.close (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
            IF ! (fr = null ) {
                 the try { 
                    fr.close (); 
                } the catch (IOException E) { 
                    E .printStackTrace (); 
                } 
            } 
        } 
    } 
    
    // use BufferedInputStream BufferedOutputStream achieve non-text files and copy 
    public  static  voidcopyFileBuffered1 (the src String, String dest) {
         // . 1, to provide read and write files 
        File file1 = new new File (the src); 
        File file2 = new new File (dest);
         // 2, to create the appropriate nodes stream: the FileInputStream, a FileOutputStream 
        the FileInputStream FIS = null ; 
        a FileOutputStream fos = null ;
         // . 3, before creating the buffer flow: BufferedInputStream, BufferedOutputStream The 
        BufferedInputStream BIS = null ; 
        BufferedOutputStream The BOS = null ;
         the try { 
            FIS= New new the FileInputStream (file1); 
            fos = new new a FileOutputStream (file2); 
            BIS = new new BufferedInputStream (FIS); 
            BOS = new new BufferedOutputStream The (fos);
             // . 4, the specific implementation of the file copy operation 
            byte [] B = new new  byte [1024 ];
             int len;
             the while (! (len = bis.read (B)) = -1 ) { 
                bos.write (B, 0 , len); 
                bos.flush (); 
            } 
        } the catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

Now he is ready seniors, so they have to s prediction time to learn, blog there are any questions you can tell me about, to share what the experience of learning together.

 

Guess you like

Origin www.cnblogs.com/HHHY/p/10964567.html