Java中FileOutputStream类的使用

文章详细介绍了Java中FileOutputStream类的作用和使用步骤,包括导入相关包,创建对象,异常处理,以及write方法的不同形式用于写入单个字节、字节数组和部分数据。还提到了如何进行换行写入和续写文件,并强调了每次使用后关闭流的重要性。
摘要由CSDN通过智能技术生成

目录

1.FileOutputStream类的作用

2.FileOutputStream类的使用步骤

(1)导入相关类的包

(2)创建FileOutputStream类对象

(3)写入数据

(4)关流


1.FileOutputStream类的作用

将数据写入到文件中去

2.FileOutputStream类的使用步骤

(1)导入相关类的包

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

(2)创建FileOutputStream类对象

此时会显示异常,可点击红色波浪线,使用快捷键alt+回车,选择第一个添加异常到方法签名,或者自己添加异常到方法签名。

即可创建FileOutputStream类对象

public class FileOutputStreamDemo {
    public static void main(String[] args) throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        
    }
}
注:
在创建字节输出流对象时,参数可以是字符串表示的路径或者是File对象
若文件不存在则会创建一个新的文件,但是要保证其父级路径存在
若文件已经存在,则会清空文件(即新写入的数据会覆盖之前的内容)

(3)写入数据

调用write方法写入数据

(a).void write(int b) 一次写入一个字节数据

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //IOException是FileNotFoundException的父类,因此只写IOException即可
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        //传入的是int类型数据,但实际写到文件中的是整数在ASCII上对应的字符
        fos.write(97);
        //相同的,抛出异常
    }
}

(b).void write(byte[] b) 一次写入一个字节数组数据

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1);
        //一次写入一个字节数组数据
    }
}

(c).void write(byte[] b,int off,int len) 一次写入一个字节数组的部分数据

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1,2,4);
        //off:起始字节索引,len:写入的字节个数

    }
}

 换行写入数据

想要实现换行写入数据,只需写入换行符即可

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        //创建一个字符串,内容为换行符
        fos.write(97);
        String str1 = "\r\n";//也可以只写\r或者\n但最好写完整
        //调用getBytes()方法,将字符串中内容转换为字节数组
        byte[] bytes1 = str1.getBytes();
        fos.write(bytes1);
        //同样的,可以用字符串的方式创建较长的数据,再调用getBytes()方法,转换为字节数组
        String str2 = "asdfghjklqwert";
        byte[] bytes2 = str2.getBytes();
        fos.write(bytes2);
    }
}

续写

想要实现续写(在文件原有数据后面接着写入新的数据),只需要在创建文件输出流对象时传入第二个参数true

 即可进行续写

(4)关流

每次使用完流之后都要关流,即断开程序与文件之间的链接

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
        byte[] bytes1 = {97,98,99,100,101};
        fos.write(bytes1,2,4);
        //关流
        fos.close();
    }
}

下面为续写的完整代码,其余代码基本上只需添加关流即可。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //创建输出流对象
        FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt",true);
        //写入续写数据
        String str = "asdfghjklqwert";
        byte[] bytes = str.getBytes();
        fos.write(bytes);
        //关流
        fos.close();
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠枬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值