原理详解:http://ifeve.com/java-io-char-buffered-filter/
BufferedInputStream字节流输入缓冲:
1 void bufferedInputStream() { 2 // BufferedInputStream带有缓冲区,多线程可见 3 // 读取中文会乱码 4 // 没有readLine() 5 File file = new File("E:\abcdata.txt"); 6 InputStream inputStream = null; 7 BufferedInputStream bis = null; 8 byte[] b = new byte[1024]; 9 int len; 10 StringBuilder sb = new StringBuilder(); 11 try { 12 inputStream = new FileInputStream(file); 13 bis = new BufferedInputStream(inputStream); 14 while ((len = bis.read(b)) != -1) { 15 String string = new String(b, 0, len, "UTF-8"); 16 sb.append(string); 17 } 18 System.out.println(sb); 19 } catch (FileNotFoundException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } catch (IOException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } finally { 26 if (bis != null) { 27 try { 28 bis.close(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 } 35 36 }
BufferedOutputStream字节流输出缓冲:
1 void bufferedOutputStream() { 2 File file = new File("E:\abcdata.txt"); 3 OutputStream outputStream = null; 4 BufferedOutputStream bos = null; 5 byte[] b = new byte[1024]; 6 String s = "测试输出!!"; 7 try { 8 outputStream = new FileOutputStream(file, true); 9 bos = new BufferedOutputStream(outputStream); 10 b = s.getBytes(); 11 bos.write(b); 12 // 刷新缓冲 13 bos.flush(); 14 } catch (FileNotFoundException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } finally { 21 if (bos != null) { 22 try { 23 bos.close(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 } 29 } 30 }
BufferedReader字符流输入缓冲:
1 void bufferedReader() { 2 File file = new File("E:\test.txt"); 3 BufferedReader br = null; 4 Reader reader = null; 5 String string; 6 StringBuilder sb = new StringBuilder(); 7 try { 8 reader = new FileReader(file); 9 br = new BufferedReader(reader); 10 while ((string = br.readLine()) != null) { 11 sb.append(string); 12 } 13 System.out.println(sb); 14 } catch (FileNotFoundException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } finally { 21 if (br != null) { 22 try { 23 br.close(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 } 29 } 30 31 }
BufferedWriter字符流输出缓冲:
1 void bufferedWriter() { 2 File file = new File("E:\test.txt"); 3 Writer writer = null; 4 BufferedWriter bw = null; 5 String string = "test 测试!!"; 6 try { 7 writer = new FileWriter(file,true); 8 bw = new BufferedWriter(writer); 9 bw.write(string); 10 bw.flush(); 11 } catch (IOException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } finally { 15 if (bw != null) { 16 try { 17 bw.close(); 18 } catch (IOException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 } 23 } 24 25 }