FileOutputStream

本文详细介绍了一个使用Java进行文件操作的例子,包括如何创建文件、写入数据到文件中,并使用了FileOutputStream流进行追加写入。同时,文章还展示了如何处理可能遇到的FileNotFoundException和IOException异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.*;

public class TestEncode {
    public static void main(String[] args) {
        //自动创建相对路径下文件
        File f = new File("fileTest.txt");
        OutputStream os = null;
        try {
            //选择流  append:true
            os = new FileOutputStream(f,true);
            String str = "hello world";
            //string.getBytes() String---->字符数组
            byte[] b = str.getBytes();
            //length
            os.write(b,0,b.length);
            //刷新缓存区
            os.flush();
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //关闭,释放资源
                if(os != null) {
                    try {
                        os.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值