- 缓冲流的优势是:
BufferedInputStream: 输入缓冲流:先从磁盘中将要读取的内容放入到内存中,再一次性从内存中取出来,避免了读一段取一段;
BufferedOutputStream: 输出缓冲流:先将要输出的内容放入到内存中,再一次性全都输出。
- 缓冲流的构造函数:
public BufferedInputStream(InputStream in) { this (in, DEFAULT_BUFFER_SIZE); }
public BufferedInputStream(InputStream in, int size) { super (in); if (size <= 0 ) { throw new IllegalArgumentException("Buffer size <= 0" ); } buf = new byte [size]; }
public BufferedOutputStream(OutputStream out) { this (out, 8192 ); }
public BufferedOutputStream(OutputStream out, int size) { super (out); if (size <= 0 ) { throw new IllegalArgumentException("Buffer size <= 0" ); } buf = new byte [size]; }
- 缓冲流关闭:
只需将缓冲流关闭,无需将节点流关闭,因为缓冲流内部会自动调用节点流关闭。
- 缓冲流需要调用flush()方法才能将内容输入或输出出来,或者调用close()方法时,也会调用flush()方法。
- 注意:
new FileOutputStream("***"); 如果参数中的路径有目录不存在,并不会自动创建目录;
此外:调用File 类的mkdir()或mkdirs()可能出现错误“Access is denied”,暂时还没解决。
- 缓冲流的应用
public class BufferedStream { public static void main(String[] args) { //BufferedStream fbs = new BufferedStream(); // fbs.writeFileWithBufferedStream("HelloBufferedOutputStream", // "/home/rding/bufferd.txt"); // fbs.readFileWithBufferedStream("/home/rding/bufferd.txt"); //fbs.writeFileWithBufferedStream("HelloWorld", "d:/rding/rding/buffered.txt"); String str = "c:\cc\cc"; System.out.println(str.lastIndexOf("\")); } public void readFileWithBufferedStream(String srcPath, int size) { InputStream inputStream = null; BufferedInputStream bufferedInputStream = null; StringBuffer sb = new StringBuffer(); try { inputStream = new FileInputStream(srcPath); bufferedInputStream = new BufferedInputStream(inputStream); byte[] byteArr = new byte[size]; int len = 0; while ((len = bufferedInputStream.read(byteArr)) != -1) { sb.append(new String(byteArr), 0, len); } System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void readFileWithBufferedStream(String srcPath) { InputStream inputStream = null; BufferedInputStream bufferedInputStream = null; try { inputStream = new FileInputStream(srcPath); bufferedInputStream = new BufferedInputStream(inputStream); int size = bufferedInputStream.available(); byte[] byteArr = new byte[size]; bufferedInputStream.read(byteArr); String str = new String(byteArr); System.out.println(str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void writeFileWithBufferedStream(String content, String destPath) { OutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null; try { // 下面的语句中,如果destPath中的目录不存在,不会自动创建目录,因此,建议使用下面的代码 // outputStream = new FileOutputStream(destPath); // 前提是输出的路径是"/"而不是"\"
File tmp = new File(destPath.substring(0, destPath.lastIndexOf("/"))); if(!tmp.exists()){ tmp.mkdirs(); } outputStream = new FileOutputStream(destPath); bufferedOutputStream = new BufferedOutputStream(outputStream); byte[] byteArr = content.getBytes(); bufferedOutputStream.write(byteArr); bufferedOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void copyFileWithBufferedStream(String srcPath, String destPath, int size) { InputStream inputStream = null; BufferedInputStream bufferedInputStream = null; OutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null; try { inputStream = new FileInputStream(srcPath); // 下面的语句中,如果destPath中的目录不存在,不会自动创建目录,因此,建议使用下面的代码 // outputStream = new FileOutputStream(destPath); File tmp = new File(destPath.substring(0, destPath.lastIndexOf("/"))); if(!tmp.exists()){ tmp.mkdirs(); } outputStream = new FileOutputStream(tmp); bufferedInputStream = new BufferedInputStream(inputStream); bufferedOutputStream = new BufferedOutputStream(outputStream); byte[] byteArr = new byte[size]; int len = 0; while ((len = bufferedInputStream.read(byteArr)) != -1) { bufferedOutputStream.write(byteArr, 0, len); } bufferedOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }