Demo03_FileOutputStream.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 Demo03_FileOutputStream
  9. * @description
  10. * @create 2025/7/25
  11. */
  12. public class Demo03_FileOutputStream {
  13. public static boolean autoWriting(String title,String context) throws IOException {
  14. //创建文件字节流
  15. FileOutputStream fos=new FileOutputStream("D:/《"+title+"》.txt");
  16. //调用作文模板类以及其子类对象
  17. EssayTemplate et=new EssayTemplate() {
  18. @Override
  19. public String body() {
  20. String s=context;
  21. return s;
  22. }
  23. };
  24. //调用作文模板对象的write方法,返回的是生成好的内容
  25. String write = et.write();
  26. //将文本内容写入文件
  27. fos.write(write.getBytes());
  28. //关闭流
  29. fos.close();
  30. return true;
  31. }
  32. public static void main(String[] args) {
  33. try {
  34. autoWriting("张三","a.....a.....a.....");
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }