​BufferedOutputStream(缓冲输出流) - 图1

实例

  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. /**
  6. * 字节输出缓冲流
  7. * @author Administrator
  8. *
  9. */
  10. public class BufferOutputStreamStudy {
  11. public static void main(String[] args) {
  12. // TODO Auto-generated method stub
  13. //定义文件对象
  14. String path = "D:\\movie\\japan\\dj\\cxk.txt";
  15. File file = new File(path);
  16. BufferedOutputStream bos = null;
  17. FileOutputStream fos = null;
  18. try {
  19. if(!file.exists()) {
  20. file.createNewFile();
  21. }
  22. //实例化流
  23. fos = new FileOutputStream(file);
  24. bos = new BufferedOutputStream(fos);
  25. //向文件写入内容
  26. String str = "它不是拍电影的,它是个运动员!!!!!!!";
  27. byte[] data = str.getBytes();
  28. //写
  29. bos.write(data);
  30. //刷新(清空)缓冲流-->低级流
  31. bos.flush();
  32. }catch (Exception e) {
  33. // TODO: handle exception
  34. e.printStackTrace();
  35. }finally {
  36. //关闭流
  37. try {
  38. bos.close();
  39. fos.close();
  40. } catch (IOException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }