Demo01_FileOutputStream.java 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package J20250725;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. /**
  6. * @author WanJl
  7. * @version 1.0
  8. * @title Demo01_FileOutputStream
  9. * @description
  10. * @create 2025/7/25
  11. */
  12. public class Demo01_FileOutputStream {
  13. /*
  14. FileNotFoundException 文件不存在异常
  15. IOException 不能输入或输出异常,是FileNotFoundException的父类
  16. */
  17. public static void main(String[] args) throws IOException {
  18. //1、创建字节输出流对象
  19. FileOutputStream fos=new FileOutputStream("D:/2025年7月25日.txt");
  20. //如果文件不存在,会帮我们创建
  21. //如果文件存在,会覆盖文件,会把文件内容清空
  22. //把指定的字节输入到此文件输出流
  23. fos.write(65);
  24. fos.write(66);
  25. for (int i = 0; i < 100; i++) {
  26. fos.write(i);
  27. }
  28. //无论前面做了什么,只要创建了IO流对象,就一定要调用的一个方法:close()
  29. //关闭流
  30. fos.close();
  31. }
  32. }