English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Java 流程控制

Java 数组

Java 面向对象(I)

Java 面向对象(II)

Java 面向对象(III)

Gestion des exceptions Java

Java 列表(List)

Java Queue(队列)

Java Map集合

Java Set集合

Java 输入输出(I/O)

Java Reader/Writer

Java 其他主题

Java FileOutputStream 类

在本教程中,我们将借助示例学习Java FileOutputStream及其方法。

java.io包的FileOutputStream类可用于将数据(以字节为单位)写入文件。

它继承了OutputStream抽象类。

在学习FileOutputStream之前,请确保了解Java file

创建一个FileOutputStream

为了创建文件输出流,我们必须首先导入java.io.FileOutputStream包。导入包后,就可以使用Java创建文件输出流。

1使用文件路径

//包括布尔型参数
FileOutputStream output = new FileOutputStream(String path, boolean value);
//不包括布尔型参数
FileOutputStream output = new FileOutputStream(String path);

在这里,我们创建了一个输出流,该输出流将链接到所指定的文件路径。

另外,value是可选的布尔参数。如果将其设置为true,则新数据将追加到文件中现有数据的末尾。否则,新数据将覆盖文件中的现有数据。

2使用文件的对象

FileOutputStream output = new FileOutputStream(File fileObject);

在这里,我们创建了一个输出流,它将链接到fileObject指定的文件。

FileOutputStream的方法

FileOutputStream类为OutputStream类中出现的不同方法提供了实现。

write()方法

  • write() - 将单写到byte文件输出流

  • write(byte[] array) - 将指定数组中的字节写入输出流

  • write(byte[] array, int start, int length)-从位置start开始将等于length的字节数写入数组的输出流

示例:FileOutputStream将数据写入文件

import java.io.FileOutputStream;
public class Main {
    public static void main(String[] args) {
        
        String data = "这是文件中的一行文本。";
        try {
            FileOutputStream output = new FileOutputStream("output.txt");
            byte[] array = data.getBytes();
            //将字节写入文件
            output.write(array);
            output.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个名为output的文件输出流。文件输出流与文件output.txt链接。

FileOutputStream output = new FileOutputStream("output.txt");

要将数据写入文件,我们使用了write()方法。

在这里,当我们运行程序时,output.txt文件将写入以下内容。

这是文件中的一行文本。

注意:程序中使用getBytes()方法将字符串转换为字节数组。

flush()方法

要清除输出流,可以使用flush()方法。此方法强制输出流将所有数据写入目标。例如,

import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = null;
        String data = "这是flush方法的演示";
        try {
            out = new FileOutputStream("flush.txt");
            //Use the write() method
            out.write(data.getBytes());
            //Use the flush() method
            out.flush();
            out.close();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

When we run the program, the fileflush.txtFilled with text data represented by string

close() method

To close the file output stream, you can use the close() method. Once this method is called, we cannot use the methods of FileOutputStream.

Other methods of FileOutputStream

MethodContent description
finalize()Ensure that the close() method is called
getChannel()Returns the object associated with FileChannel and output stream
getFD()Returns the file descriptor associated with the output stream