【Java学习记录-13】字节流

IO流

IO流概述

在Java中,IO流是Input/Output Stream的简称,是用于处理数据流的类库。IO流分为字节流和字符流两类,分别用于处理二进制数据和文本数据。

在Java中,每当需要读取或写入数据时,都需要使用IO流来处理数据。IO流提供了多种形式的读写方法,包括文件读写、网络通信、内存读写等等。其中,字节流适用于读写二进制数据,字符流适用于读写文本数据。

IO流分类

在Java中,IO流主要定义在java.io包中,主要包含以下四个类:

  • InputStream:输入字节流,类似于从文件或网络读取二进制数据。
  • OutputStream:输出字节流,类似于将二进制数据写入到文件或网络中。
  • Reader:输入字符流,类似于从文件或网络读取文本数据。
  • Writer:输出字符流,类似于将文本数据写入到文件或网络中。

在使用IO流时,需要先创建对应的输入/输出流对象,并使用相应的方法来读写数据。例如,使用FileInputStream和FileOutputStream来读写文件的字节流,使用FileReader和FileWriter来读写文件的字符流。

字节流

概述

在Java中,字节流是以二进制方式处理数据的输入输出流。字节流以字节为单位进行读写操作,适用于处理二进制数据或文本文件中的字节数据。

举例

例如,可以通过FileInputStream和FileOutputStream类来读写文件的字节流:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamExample {
    public static void main(String[] args) {
        String sourceFilePath = "/path/to/source/file";
        String destinationFilePath = "/path/to/destination/file";

        try (FileInputStream fis = new FileInputStream(sourceFilePath);
             FileOutputStream fos = new FileOutputStream(destinationFilePath)) {  // 创建输入输出流对象
            byte[] buffer = new byte[1024];  // 定义缓冲区大小
            int length;
            while ((length = fis.read(buffer)) > 0) {  // 读取数据并写入目的地文件
                fos.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上面的代码通过FileInputStream和FileOutputStream类的组合来实现文件的复制。在while循环中,每次从源文件中读取1024个字节到缓冲区,然后将缓冲区中的数据写入到目标文件,直到读取完毕为止。

字节流写数据的3种方式

在 Java 中,可以使用以下三种方式将字节写入流:

  1. 使用 write 方法

字节流的 OutputStream 和其子类都有 write 方法,可以将数据写入输出流。例如:

OutputStream outputStream = new FileOutputStream("path/to/file.txt");
byte[] data = "Hello, world!".getBytes();
outputStream.write(data);
outputStream.close();
  1. 使用 BufferedOutputStream

BufferedOutputStream 是 OutputStream 的子类,可以使用它来缓存写入的数据。例如:

OutputStream outputStream = new FileOutputStream("path/to/file.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] data = "Hello, world!".getBytes();
bufferedOutputStream.write(data);
bufferedOutputStream.close();
  1. 使用 DataOutputStream

DataOutputStream 可以按照指定的格式将数据写入输出流,例如按照字符序列、16 位整数等格式写入。例如:

OutputStream outputStream = new FileOutputStream("path/to/file.txt");
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF("Hello, world!");
dataOutputStream.close();

需要注意的是,无论使用哪种方式,都需要在结束时调用 close 方法关闭输出流。如果一直没有关闭输出流,可能会出现数据丢失或锁定文件等问题。

字节流读数据

在Java中,你可以使用InputStream类及其子类来实现字节流的数据读取。以下是一个简单的示例代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ByteStreamExample {
    public static void main(String[] args) {
        String filePath = "path/to/file.txt"; // 替换为你的文件路径
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(filePath);
            byte[] buffer = new byte[1024]; // 缓冲区大小,可以根据需要进行调整
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                // 如果bytesRead的值不为-1,表示还有数据可读,处理读取到的数据
                processBytes(buffer, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void processBytes(byte[] buffer, int bytesRead) {
        // 在这里进行对读取到的字节数据的处理
        // 例如,可以将字节数据转换为字符串进行输出或进行其他操作
        String data = new String(buffer, 0, bytesRead);
        System.out.print(data);
    }
}

字节流复制文本文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopyExample {
    public static void main(String[] args) {
        String sourceFilePath = "path/to/source/file.txt"; // 源文件路径
        String targetFilePath = "path/to/target/file.txt"; // 目标文件路径

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            inputStream = new FileInputStream(sourceFilePath);
            outputStream = new FileOutputStream(targetFilePath);

            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            System.out.println("文件复制成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在上述代码中,我们使用FileInputStream打开源文件的输入流,使用FileOutputStream打开目标文件的输出流。我们定义了一个缓冲区buffer,用于存储从输入流读取的字节数据。然后,我们使用inputStream.read(buffer)从输入流中读取数据,并使用outputStream.write(buffer, 0, bytesRead)将读取的数据写入输出流。

通过循环重复读取和写入操作,直到源文件的所有数据都被复制到目标文件中。最后,我们在控制台打印出"文件复制成功!"的消息。

请确保将示例代码中的sourceFilePath和targetFilePath替换为实际的源文件和目标文件的路径。

你可能感兴趣的:(java,学习,python)