JAVA I / O of file copy

There is no big brother told me not to insert the code area (for now this sentence region) how deleted. . . . . . .
        // byte by byte copy 
        public  static   void   Fun () throws IOException { 
            the FileInputStream FIS = new new the FileInputStream ( "F.: /Abc.txt" ); 
            a FileOutputStream fos = new new a FileOutputStream ( "F.: / Byte stream replication ( a byte by byte) .txt " );
             int by = 0 ;
             the while ! ((= fis.read by ()) = -1 ) { 
                fos.write (by); 
            } 
            fis.close (); 
            fos. Close (); 
        } 
        // 1024 copy byte array (array buffer was added) 
        public   static void  fun1() throws IOException {
            FileInputStream fis = new FileInputStream("F:/abc.txt");
            FileOutputStream fos = new FileOutputStream("F:/字节流复制(1024字节数组).txt");
            int len = 0;
            byte[] bytes =new byte[1024];
            while ((len=fis.read(bytes)) != -1) {
                fos.write(bytes,0,len);
            }
            fis.close();
            fos.close();
        }
        //And copy a byte by byte with the buffered stream 
        public  static    void   fun2 () throws IOException { 
            BufferedInputStream BIS = new new BufferedInputStream ( new new the FileInputStream ( "F.: /Abc.txt" )); 
            BufferedOutputStream The BOS = new new BufferedOutputStream The ( new new a FileOutputStream ( " F: / buffer byte stream replication (byte by byte) .txt " )); 
    
            int by = 0 ;
             the while ((= bis.read by ()) = -1! ) { 
                bos.write (by) ; 
            } 
            bos.close (); 
            bis.close (); 
        }
        // 1024 byte array and copied by the stream buffer (array buffer was added) 
        public   static  void   FUN3 () throws IOException { 
            BufferedInputStream BIS = new new BufferedInputStream ( new new the FileInputStream ( "F.: /Abc.txt" )); 
            BufferedOutputStream The BOS = new new BufferedOutputStream The ( new new a FileOutputStream ( "F.: / copy byte stream buffer (1024 byte array) .txt" ));
             int len = 0 ;
             byte [] bytes = new new  byte [1024 ];
             the while ((len = bis.read (bytes))! = -1 ) {
                bos.write (bytes, 0 , len); 
            } 
            bos.close (); 
            bis.close (); 
        } 
        // character buffer copy packet stream (line by line format can be reserved) 
        public   static  void   FUN4 () throws IOException { 
            the FileInputStream FileInputStream = new new FileInputStream ( "F: /abc.txt" );
       // reason garbled problem: encoding file encoding system, the default java coding of conflict.
        // If we use these FileReader class to read the file system, it calls the java default character encoding is UTF-8, but in general the TXT WINDOW default is ANSI, and its own native Chinese encoding is GBK 
            InputStreamReader InputStreamReader = new new InputStreamReader (fileInputStream, "GBK");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
              
            FileOutputStream fileOutputStream = new FileOutputStream("F:/字符缓冲流复制文件.txt");
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
              
              String line = null;
            while (null != (line = bufferedReader.readLine())) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
            
            if (bufferedWriter != null || outputStreamWriter != null || fileOutputStream != null) {
                bufferedWriter.close();
                outputStreamWriter.close();
                fileOutputStream.close();
            }
            if (bufferedReader != null || inputStreamReader != null || fileInputStream != null) {
                bufferedReader.close();
                inputStreamReader.close();
                fileInputStream.close (); 
            }      
        } 

        // byte stream (A ByteArrayInputStream) is less than when you enough resources, the best choice is the choice BufferedOutputStream, when you select a job done quickly, you can select the output stream https ByteArrayOutputStream like: // www.cnblogs.com/yixiu868/p/8144670.html 
        public   static  void   fun5 () throws IOException { 
              the FileInputStream FileInputStream = new new the FileInputStream ( "F.: /abc.txt" );
             byte [] Buffer = new new  byte [1024 ] ;
             int len = 0 ; 
            ByteArrayOutputStream BOS = new new ByteArrayOutputStream ();
             the while ((len = fileInputStream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
            byte[] bytes = bos.toByteArray();
            FileOutputStream fileOutputStream = new FileOutputStream("F:/字节流(ByteArrayInputStream).txt");
            fileOutputStream.write(bytes);
            
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close (); 
            } 
        } 
          public  static  void main (String [] Arg) throws IOException { 
                     
            Long Start, End; 
            Start = System.currentTimeMillis (); 
            Fun (); 
            End = System.currentTimeMillis (); 
            the System.out. the println ( "copy a byte by byte (byte stream) takes time:" + (End - Start) + "MS" ); 
           
            Start = System.currentTimeMillis (); 
            fun1 (); 
            End = System.currentTimeMillis ( ); 
            System.out.println ("Copy 1024 byte array (byte stream) takes time:" + (End - Start) + "MS" );
             
            Start = System.currentTimeMillis (); 
            fun2 (); 
            End = System.currentTimeMillis (); 
            the System.out. the println ( "copy a byte by byte (buffered stream) takes time:" + (End - Start) + "MS" ); 
            
            Start = System.currentTimeMillis (); 
            FUN3 (); 
            End = System.currentTimeMillis () ; 
            System.out.println ( "copy 1024 byte array (buffer flow) takes time:" + (End - Start) + "MS" ); 
            
            Start = System.currentTimeMillis (); 
            FUN4 ();
            end =System.currentTimeMillis (); 
            System.out.println ( "character buffer stream spend time copying files:" + (End - Start) + "MS" ); 
            
            Start = System.currentTimeMillis (); 
            fun5 (); 
            End = the System. with currentTimeMillis (); 
            System.out.println ( "time spent byte stream (A ByteArrayInputStream):" + (End - Start) + "MS" ); 

          }

 


Results of the:

Byte stream and a stream of characters in the IO below shows, each of which is divided into the corresponding input and output streams. Need to understand the basic concepts: character stream (Reader, Writer), byte stream, the stream buffer (Buffered); relevant information according to their needs can use to search correspondence.

Guess you like

Origin www.cnblogs.com/hanzhiyong/p/11082774.html