java通过字节流输出字符串及文件及文件夹样例参考

    public static void main(String[] args) throws Exception {

        //字节流
        byteOutStream();


        //字符流 (输出流中含有中文时使用字符流)
        charOutStream();

    }
    public static void charOutStream() throws Exception{
        // 1:利用File类找到要操作的对象
        File file = new File("c:" + File.separator + "demo" + File.separator + "tt.txt");
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }

        //2:准备输出流
        Writer out = new FileWriter(file);
        out.write("测试, 哈哈");
        out.close();

    }

    public static void byteOutStream() throws Exception {

        //1:使用File类创建一个要操作的文件路径
        File file = new File("c:" + File.separator + "demo1" + File.separator + "test.txt");
        if(!file.getParentFile().exists()){ //如果文件的目录不存在
            file.getParentFile().mkdirs(); //创建目录

        }

        //2: 实例化OutputString 对象
        OutputStream output = new FileOutputStream(file);

        //3: 准备好实现内容的输出

        String msg = "HelloWorld";
        //将字符串变为字节数组
        byte data[] = msg.getBytes();
        output.write(data);
        //4: 资源操作的最后必须关闭
        output.close();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值