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();
}
}
}
Java IO练习--在程序中写一个“HelloJavaWorld你好世界“输出到操作系统文件Hello.txt文件中
Java程序写入Hello.txt:文件操作与IO流实践
最新推荐文章于 2023-08-05 10:29:58 发布
644





