字符流和字节流的主要区别:
1.字节流读取的时候,读到一个字节就返回一个字节; 字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时。先去查指定的编码表,将查到的字符返回。
2.字节流可以处理所有类型数据,如:图片,MP3,AVI视频文件,而字符流只能处理字符数据。只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流。
字节流:
1、通过字节流写数据
code:
1 package com.test; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 8 9 public class WriteByteStream { 10 11 public static void main(String[] args) { 12 try { 13 14 15 FileOutputStream fos = new FileOutputStream("textw.txt"); 16 String outString = "write 123456写出数据"; 17 byte output[] = outString.getBytes("UTF-8"); 18 fos.write(output); 19 fos.close(); 20 21 22 } catch (FileNotFoundException e) { 23 e.printStackTrace(); 24 } catch (UnsupportedEncodingException e) { 25 e.printStackTrace(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 }
2、通过字节流读数据:
code:
1 package com.test; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.UnsupportedEncodingException; 7 8 9 10 public class ReadByteStream { 11 12 public static void main(String[] args) { 13 try { 14 FileInputStream fis = new FileInputStream("text.txt"); 15 byte input[] = new byte[21]; 16 fis.read(input); 17 18 String inputString = new String(input, "UTF-8"); 19 System.out.println(inputString); 20 fis.close(); 21 22 } catch (FileNotFoundException e) { 23 e.printStackTrace(); 24 } catch (UnsupportedEncodingException e) { 25 e.printStackTrace(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 30 } 31 32 }
3、使用字节流读写数据
code:
1 package com.test; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 9 public class CopyByByteStream { 10 11 public static void main(String[] args) { 12 try { 13 14 15 16 FileInputStream fis = new FileInputStream("movie.mp4"); 17 FileOutputStream fos = new FileOutputStream("movie_new.mp4"); 18 19 byte input[] = new byte[50]; 20 long before = System.currentTimeMillis(); 21 int count = 0; 22 while (fis.read(input) != -1) { 23 fos.write(input); 24 count++; 25 } 26 fos.flush(); 27 fis.close(); 28 fos.close(); 29 System.out.println(System.currentTimeMillis()-before+"ms"); 30 System.out.println("done"); 31 32 33 34 } catch (FileNotFoundException e) { 35 e.printStackTrace(); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 }
4、使用带缓冲的字节流读写数据(字节流模板)
code:
1 package com.test; 2 import java.io.BufferedInputStream; 3 import java.io.BufferedOutputStream; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 10 11 public class ReadByBufferedByteStream { 12 13 public static void main(String[] args) { 14 try { 15 16 17 FileInputStream fis = new FileInputStream("movie.mp4"); 18 BufferedInputStream bis = new BufferedInputStream(fis,1000000); 19 FileOutputStream fos = new FileOutputStream("moive_new.mp4"); 20 BufferedOutputStream bos = new BufferedOutputStream(fos,1000000); 21 //大型文件对应的数组可以大一些,小文件对应的数组小一些 22 byte input[] = new byte[100000]; 23 int count = 0; 24 long before = System.currentTimeMillis(); 25 while (bis.read(input) != -1) { 26 bos.write(input); 27 count++; 28 } 29 bos.flush(); 30 bis.close(); 31 fis.close(); 32 bos.close(); 33 fos.close(); 34 System.out.println(System.currentTimeMillis()-before+"ms"); 35 System.out.println("读取了:"+count+"次"); 36 37 38 39 } catch (FileNotFoundException e) { 40 e.printStackTrace(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 } 45 46 }
字符流:
1、使用字符流读写数据
code:
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 import java.io.UnsupportedEncodingException; 9 10 11 public class RWByCharStream { 12 13 public static void main(String[] args) { 14 try { 15 16 17 //File file = new File("java.txt"); 18 FileInputStream fis = new FileInputStream("java.txt"); 19 FileOutputStream fos = new FileOutputStream("java_new.txt"); 20 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 21 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); 22 char input[] = new char[100]; 23 int l = 0; 24 while ((l = isr.read(input)) != -1) { 25 //String inputString = new String(input,0,l); 26 osw.write(input,0,l); 27 } 28 isr.close(); 29 fis.close(); 30 osw.close(); 31 fos.close(); 32 System.out.println("done"); 33 34 } catch (FileNotFoundException e) { 35 e.printStackTrace(); 36 } catch (UnsupportedEncodingException e) { 37 e.printStackTrace(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 42 } 43 44 }
2、使用带有缓冲的字符流读写数据(字符流模板)
code:
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 import java.io.PrintWriter; 10 import java.io.UnsupportedEncodingException; 11 12 13 public class RWByBufferedCharStream { 14 15 public static void main(String[] args) { 16 try { 17 18 19 //File file = new File("java.txt"); 20 FileInputStream fis = new FileInputStream("java.txt"); 21 FileOutputStream fos = new FileOutputStream("java_new_buff.txt"); 22 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 23 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); 24 25 BufferedReader br = new BufferedReader(isr); 26 //BufferedWriter bw = new BufferedWriter(osw); 27 PrintWriter pw = new PrintWriter(osw,true); 28 29 String input; 30 while ((input = br.readLine()) != null) { 31 //bw.write(input); 32 pw.println(input); 33 } 34 br.close(); 35 //bw.flush();bw.close(); 36 pw.close(); 37 isr.close(); 38 fis.close(); 39 osw.close(); 40 fos.close(); 41 System.out.println("done"); 42 43 } catch (FileNotFoundException e) { 44 e.printStackTrace(); 45 } catch (UnsupportedEncodingException e) { 46 e.printStackTrace(); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 } 51 52 }