1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package J20250725;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo03_FileOutputStream
- * @description
- * @create 2025/7/25
- */
- public class Demo03_FileOutputStream {
- public static boolean autoWriting(String title,String context) throws IOException {
- //创建文件字节流
- FileOutputStream fos=new FileOutputStream("D:/《"+title+"》.txt");
- //调用作文模板类以及其子类对象
- EssayTemplate et=new EssayTemplate() {
- @Override
- public String body() {
- String s=context;
- return s;
- }
- };
- //调用作文模板对象的write方法,返回的是生成好的内容
- String write = et.write();
- //将文本内容写入文件
- fos.write(write.getBytes());
- //关闭流
- fos.close();
- return true;
- }
- public static void main(String[] args) {
- try {
- autoWriting("张三","a.....a.....a.....");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|