字节输出流对文件的写 *** 作

字节输出流对文件的写 *** 作,第1张

字节输出流对文件的写 *** 作
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//通过字节输出流完成对文件的写 *** 作
public class FileOutputSrteamDemo {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //创建字节输出流对象 FileOutputStream
        //    fos =new FileOutputStream("D:\chenxuyuan\test.txt");
            //不覆盖文件内容,从文件内容末尾追加新内容
            fos = new FileOutputStream("D:\chenxuyuan\test.txt,",true);
            //调用输出流对象FileOutputStream的write()方法写入文件
            String info = "新的一句话";
            //将写入的字符串打散为一个字节数组
            byte [] infos = info.getBytes();
            fos.write(infos,0,infos.length);
            System.out.println("test文件已更新");
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //关闭输出流
            try {
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5661302.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存