Byte stream and character stream flow of java IO

In fact, learning the basic document file type, byte stream and character stream back are particularly simple, you first need to know the difference between a byte stream and character stream

Byte stream:

  For transmitting image, various documents, large files, text is transmitted through the byte stream.

Jifuryu: 

  You can only read text messages

Operation byte stream interface class

  1, InputStream input stream of bytes

  2, outputStream output stream of bytes

  3, FileinputStream input stream of bytes instantiated

  4, FileoutputStream output stream of bytes instantiated

  5, BufferedInputStream enhanced version of the input stream, the input buffer when used for large file transfers

  6, BufferedOutputStream enhanced version of the output stream, the output buffer when used for large file transfers

    / ** 
     * Enhanced file copy (most used) will file several G 10 seconds 
     * 
     * @param path to file incoming file 
     * @return 
     * / 
    public  static  boolean the Read (File file) { 

        // file file = new new file ( "D: \\ 1.txt"); 

        IF (file == null || file.isFile ()) { 
            System.err.println ( "file not empty" );
             return  to false ; 
        } 

        BOS BufferedOutputStream The = null ; 
        BufferedInputStream BIS = null ;
         the try {
            bis = new BufferedInputStream(new FileInputStream(file)); // 获取文件流
            bos = new BufferedOutputStream(
                    new FileOutputStream("D:\\video\\AdminVideo\\PrivateVideo\\" + file.getName())); // 转存为...

            int len = 0;
            byte[] b = new byte[1024000];
            while (-1 < (len = bis.read(b))) {
                bos.write(b, 0, len);
            }
            bos.flush();
            System.err.println("D:\\video\\AdminVideo\\PrivateVideo\\" + file.getName());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * Read a text character stream cycle 
     *@throws IOException
      * / 
    public  static  void file2 () throws IOException {
         // definition file path 
        File F = new new File ( "D:" + + File.pathSeparator "test.txt" );
         //   definition of the output character stream 
        Reader reader = new new the FileReader (F);
         int len = 0 ;
         char [] = C new new  char [1024 ];
         int TEMP = 0 ;
         // reading character files in a cyclic manner by 
        the while ((TEMP = reader.Read ())! = -1 ) {
            C [len] = ( char ) TEMP; 
            len ++ ; 
        } 
        // close the character stream, or will be error 
        reader.Close (); 
        System.out.println ( "content:" + new new String (C, 0 , len )); 
    }

Very simple example, we want to help you learn. I do not know can leave a message in the comments section and I will get back to you as soon as possible

Guess you like

Origin www.cnblogs.com/yonim/p/9477762.html