java stream buffer copy files

  • Byte buffer

  public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src\\1.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\1_copy.jpg"));
        int len=0;
        byte[] bytes = new byte[1024];
        while((len =bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();

        long end = System.currentTimeMillis();
        System.out.println("共耗时:"+(end-start)+"s");
    }
  • Character buffer

  For Chinese, a character may be equal due to the different encoding two bytes, three bytes may also be equal. A stream of characters solve the garbage problem Chinese conversion.

        long start = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new FileReader("src\\buffer2.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("src\\buffer2_copy.txt"));
        String line ;
        while((line=br.readLine())!=null){
            System.out.println(line);
            bw.write(line);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        br.close();
        long end = System.currentTimeMillis();
        System.out.println("共耗时:"+(end-start)+"s");
    }

 

Guess you like

Origin www.cnblogs.com/Nora-F/p/11059495.html