package com.kj.test; import cn.hutool.core.io.IoUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * 在程序中写一个"HelloJavaWorld你好世界"输出到操作系统文件Hello.txt文件中 */ public class FileTest4 { /** * 分析,写入文件,需要乃至FileOutputStream */ public static void main(String[] args) { // 向D:/HelloWord.txt中写入内容,文件不存在会自动创建 File file = new File("D:\\HelloWord.txt"); try{ // 创建输出流 FileOutputStream fos = new FileOutputStream(file); // 把String类型的字符串转化为byte数组保存在输出流中 fos.write("Hello World 世界,你好".getBytes()); fos.flush(); IoUtil.close(fos); } catch (IOException e){ e.getMessage(); } } }