java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java FileOutputStream类

Java中FileOutputStream类的使用

作者:楠枬

java.io.FileOutputStream类是文件输出流,用于将数据写出到文件,下面就来介绍一下Java中FileOutputStream类的使用,具有一定的参考价值,感兴趣的可以了解一下

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();
    }
}

到此这篇关于Java中FileOutputStream类的使用的文章就介绍到这了,更多相关Java FileOutputStream类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文