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"); }
对于中文,一个字符因为编码不一样可能等于两个字节,也有可能等于三个字节。字符流解决了中文转换的乱码问题。编码
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"); }