超详细的逐句介绍Java高级接口之文件输入流函数FileOutputStream函数源码讲解(全)

一、FileOutputStream函数

FileOutputStream函数主要实现将程序的内容通过这个接口将数据传入到文件中。下面我将从源码角度进行对FileOutputStream的内部方法进行介绍。

二、内部源码介绍

FileOutputStream函数主要继承于OutputStream函数

public
class FileOutputStream extends OutputStream
{
}

定义了文件描述字段

private final FileDescriptor fd;

定义是否是追加模式

private final boolean append;

定义文件通道

private FileChannel channel;

定义文件路径path

private final String path;

下面定义了一个关锁对象和一个转瞬即逝的关锁标志符

private final Object closeLock = new Object();
private volatile boolean closed = false;

创建文件输出流函数

public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
    }

创建文件输入流函数,isAppend代表是否以追加模式写入

public FileOutputStream(String name, boolean append)
        throws FileNotFoundException
    {
        this(name != null ? new File(name) : null, append);
    }

文件输出流函数的实现方法

public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
    }

定义文件输出流函数,是以追加方式写入的方法

    public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
    {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        fd.attach(this);
        this.append = append;
        this.path = name;

        open(name, append);
    }

用文件描述字段打开文件输出流函数

    public FileOutputStream(FileDescriptor fdObj) {
        SecurityManager security = System.getSecurityManager();
        if (fdObj == null) {
            throw new NullPointerException();
        }
        if (security != null) {
            security.checkWrite(fdObj);
        }
        this.fd = fdObj;
        this.append = false;
        this.path = null;

        fd.attach(this);
    }

下面调用JVM实现打开文件的方法定义

    private native void open0(String name, boolean append)
        throws FileNotFoundException;

打开文件并以追加方式写入数据

private void open(String name, boolean append)
        throws FileNotFoundException {
        open0(name, append);
    }

调用JVM实现以追加方式写入数据

private native void write(int b, boolean append) throws IOException;

write的一个实现类,向文件中写入一个整型数据

public void write(int b) throws IOException {
        write(b, append);
    }

调用JVM实现向文件中写入byte数组

private native void writeBytes(byte b[], int off, int len, boolean append)
        throws IOException;

向文件中写入byte数组的实现类

public void write(byte b[]) throws IOException {
        writeBytes(b, 0, b.length, append);
    }

向文件中写入byte数组的某一部分

public void write(byte b[], int off, int len) throws IOException {
        writeBytes(b, off, len, append);
    }

调用JVM实现文件关闭

private native void close0() throws IOException;

实现关闭文件输出流函数

public void close() throws IOException {
        synchronized (closeLock) {
            if (closed) {
                return;
            }
            closed = true;
        }

        if (channel != null) {
            channel.close();
        }

        fd.closeAll(new Closeable() {
            public void close() throws IOException {
               close0();
           }
        });
    }

定义获取文件描述字符

public final FileDescriptor getFD()  throws IOException {
        if (fd != null) {
            return fd;
        }
        throw new IOException();
     }

定义实现文件管道

public FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, false, true, append, this);
            }
            return channel;
        }
    }

清除文件连接,彻底释放文件

protected void finalize() throws IOException {
        if (fd != null) {
            if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
                flush();
            } else {
                /* if fd is shared, the references in FileDescriptor
                 * will ensure that finalizer is only called when
                 * safe to do so. All references using the fd have
                 * become unreachable. We can call close()
                 */
                close();
            }
        }
    }

定义初始化ID

private static native void initIDs();

静态代码块,获取初始化ID

 static {
        initIDs();
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 文件字节IOJava中用于读写文件的一种方式,可以通过字节来读取和写入文件。合并文件是将多个文件合并成一个文件的过程,可以使用文件字节IO来实现。 具体实现步骤如下: 1. 创建一个输出,用于将多个文件合并成一个文件。 2. 创建多个输入,用于读取多个文件的内容。 3. 从每个输入中读取数据,并将数据写入输出中。 4. 关闭输入和输出。 示例代码如下: ```java import java.io.*; public class MergeFiles { public static void main(String[] args) { try { // 创建输出 FileOutputStream fos = new FileOutputStream("merged.txt"); // 创建输入 FileInputStream fis1 = new FileInputStream("file1.txt"); FileInputStream fis2 = new FileInputStream("file2.txt"); // 合并文件 byte[] buffer = new byte[1024]; int len; while ((len = fis1.read(buffer)) != -1) { fos.write(buffer, , len); } while ((len = fis2.read(buffer)) != -1) { fos.write(buffer, , len); } // 关闭 fis1.close(); fis2.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码将文件file1.txt和file2.txt合并成一个文件merged.txt。在实际应用中,可以根据需要合并多个文件。 ### 回答2: 文件合并是指将多个文件的内容合并到一个文件中,常用于将多个小文件合并成一个大文件或将分割的大文件恢复成原始文件。在Java中,可以使用文件字节IO实现文件的读写和合并。 文件字节IOJava中最基本的IO,它通过字节来读写文件,常用的类有FileInputStream、FileOutputStream、RandomAccessFile等。需要注意的是,文件合并必须确保合并后的文件占用空间足够存储文件的内容,否则会导致数据丢失。 实现文件合并的步骤如下: 1. 创建一个输出,作为合并后文件的输出。 2. 创建一个字节数组,用于存储文件读取的数据。 3. 循环读取每个文件,将读取的数据保存到字节数组中。 4. 每读取完一个文件,就将字节数组中的数据写入输出中。 5. 重复步骤3和步骤4,直到所有文件的内容都被读取且写入到输出中。 6. 关闭所有的输入和输出,完成文件合并。 下面是一个文件合并的示例代码: ``` public class FileMerge { public static void merge(String[] files, String target) { try { OutputStream outputStream = new FileOutputStream(target); byte[] buffer = new byte[1024]; int length; for (String file : files) { InputStream inputStream = new FileInputStream(file); while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } inputStream.close(); } outputStream.close(); } catch (IOException e) { System.out.println("文件合并出错:" + e.getMessage()); } } } ``` 以上代码中,merge方法接收一个字符串数组files和一个目标文件名target,files为需要合并的多个文件文件名数组,target为合并后的目标文件名。 在方法中,首先创建一个输出outputStream,用于写入目标文件的内容。然后创建一个字节数组buffer,用于存储文件读取的数据。接下来使用for循环遍历所有的文件,读取每个文件的内容并保存到字节数组buffer中,然后使用输出outputStream将buffer中的数据写入到目标文件中,重复读取并写入操作,直到所有文件的内容都被合并到目标文件中。最后关闭所有的输入和输出。 总之,文件合并是Java中常用的操作之一,使用文件字节IO可以快速实现。同时需要注意文件大小和空间,确保合并后的文件可以存储所有文件的内容,避免数据丢失。 ### 回答3: Java提供了许多操作文件的类和方法,其中文件字节IO允许我们按字节读写文件内容。在文件处理过程中,合并多个文件成为一个文件的操作是非常常见的。本文将介绍如何使用Java文件字节IO来合并文件。 首先,我们需要打开需要合并的文件并创建一个输出文件。代码如下: ``` FileInputStream fis1 = new FileInputStream("file1.txt"); FileInputStream fis2 = new FileInputStream("file2.txt"); OutputStream os = new FileOutputStream("output.txt"); ``` 接着,我们需要定义一个缓存区,该缓存区将逐个字节地从输入文件中读取内容并将其写入输出文件。代码如下: ``` byte[] buffer = new byte[1024]; int length; while ((length = fis1.read(buffer)) > 0) { os.write(buffer, 0, length); } while ((length = fis2.read(buffer)) > 0) { os.write(buffer, 0, length); } ``` 上述代码将file1.txt和file2.txt中的内容逐个字节地读取到缓存区中,并写入到输出文件output.txt中。这个过程将会把所有需要合并的文件部合并到output.txt文件当中。 最后,我们需要关闭所有的对象,释放系统资。代码如下: ``` fis1.close(); fis2.close(); os.close(); ``` 总体而言,将这些文件合并起来并不难,只需要按照上述步骤打开输入文件、创建输出文件、定义一个缓存区、逐个字节地读取内容并将其输出到输出文件中即可。 在实际编程中,我们可以编写一个方法来自动合并文件。该方法将输入一个文件列表和输出文件路径,并将所有文件的内容合并到该输出路径中。这些代码可以用于处理非常大的文件,因为可以处理多个文件,并且只需要一次性读取和写入缓存区。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

绝域时空

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

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

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

打赏作者

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

抵扣说明:

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

余额充值