Java IO基础知识点总结

93 篇文章 2 订阅

基础概念

Java IO(输入/输出)是 Java 编程中一个核心概念,它涉及到 Java 应用程序与外部环境(如文件系统、网络等)之间的数据交换。Java IO 类库主要在 java.io 包中,提供了丰富的类和接口来处理数据流和文件操作。

1. 基本概念和流(Stream)分类

Java IO 操作主要基于流的概念,流(Stream)是数据传输的抽象。在 Java 中,流分为两大类:字节流(Byte Streams)和字符流(Character Streams)。

字节流:以字节(8位二进制)为单位处理数据,主要用于处理原始二进制数据,如图像、声音等文件。主要类包括 InputStream 和 OutputStream。
字符流:以字符为单位处理数据,适用于处理文本数据。主要类包括 Reader 和 Writer。

2. 字节流和字符流

这两种流在 Java 中的使用方法类似,但它们服务的数据类型和使用场景不同。
字节流

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

public class ByteStreamTest {
    public static void main(String[] args) {
        try (FileInputStream in = new FileInputStream("input.txt");
             FileOutputStream out = new FileOutputStream("output.txt")) {
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
字符流
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreamTest {
    public static void main(String[] args) {
        try (FileReader in = new FileReader("input.txt");
             FileWriter out = new FileWriter("output.txt")) {
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. 缓冲流

Java IO 提供了缓冲流(Buffered Streams),它们使用内部缓冲区来读写数据,这样可以减少实际的读写操作次数,从而提高性能。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedStreamTest {
    public static void main(String[] args) {
        try (BufferedReader in = new BufferedReader(new FileReader("input.txt"));
             BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"))) {
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line);
                out.newLine();  // 写入换行符
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. 数据流

数据流(Data Streams)允许应用程序以与机器无关的方式从底层输入流中读取基本数据类型和字符串。

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

public class DataStreamTest {
    public static void main(String[] args) {
        try (DataOutputStream out = new DataOutputStream(new FileOutputStream("data.bin"));
             DataInputStream in = new DataInputStream(new FileInputStream("data.bin"))) {
            out.writeInt(123);
            out.writeDouble(123.45);
            out.writeUTF("Hello");

            int i = in.readInt();
            double d = in.readDouble();
            String s = in.readUTF();

            System.out.println("Read Int: " + i);
            System.out.println("Read Double: " + d);
            System.out.println("Read String: " + s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

进阶概念

1. 随机访问文件

Java IO 提供了 RandomAccessFile 类,允许你跳到文件中的任何位置来读写数据。这对于需要访问大文件中特定部分的应用特别有用。

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileExample {
    public static void main(String[] args) {
        try (RandomAccessFile file = new RandomAccessFile("example.dat", "rw")) {
            // 写入一些数据
            file.writeInt(100);
            file.writeDouble(123.456);
            file.writeUTF("Hello World");

            // 跳到文件的起始位置
            file.seek(0);

            // 读取并显示数据
            System.out.println(file.readInt());
            System.out.println(file.readDouble());
            System.out.println(file.readUTF());

            // 跳到文件的开头,写入新的整数
            file.seek(0);
            file.writeInt(200);

            // 再次跳到起始位置,读取新的整数
            file.seek(0);
            System.out.println(file.readInt());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. Java NIO

Java NIO (New IO) 是一个从 Java 1.4 开始引入的API,它提供了非阻塞方式的IO。NIO 支持面向缓冲的IO操作,NIO 的通道(Channel)和缓冲区(Buffer)使得数据的读写更为高效。

Channels and Buffers
Channels:类似于流,但可以进行双向数据传输(读写)。
Buffers:缓冲区实质上是数据的容器,并且可以被读写。

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOExample {
    public static void main(String[] args) {
        try (FileInputStream fin = new FileInputStream("input.txt");
             FileChannel channel = fin.getChannel()) {
            
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int bytesRead = channel.read(buffer);
            
            while (bytesRead != -1) {
                buffer.flip();  // 切换到读模式
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());
                }
                buffer.clear();  // 清空缓冲区,准备再次读取
                bytesRead = channel.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. 选择器(Selectors)

选择器是Java NIO中的高级功能,允许单个线程管理多个Channel。如果你的应用开启了多个连接(例如,一个聊天服务器),使用选择器可以非常高效地管理这些连接。

import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SelectionKey;
import java.net.InetSocketAddress;
import java.util.Iterator;

public class SelectorExample {
    public static void main(String[] args) throws IOException {
        try (Selector selector = Selector.open();
             ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
            
            serverChannel.bind(new InetSocketAddress("localhost", 8080));
            serverChannel.configureBlocking(false);
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);

            while (true) {
                selector.select();  // 阻塞,直到至少有一个通道发生了注册的事件
                Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
                
                while (keyIterator.hasNext()) {
                    SelectionKey key = keyIterator.next();
                    if (key.isAcceptable()) {
                        // 处理接受新连接
                    } else if (key.isReadable()) {
                        // 处理读事件
                    }
                    keyIterator.remove();
                }
            }
        }
    }
}

4. 文件锁定(File Locking)

文件锁定是一种同步访问文件的机制,确保没有两个进程可以同时写入同一个文件。

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileLockExample {
    public static void main(String[] args) {
        try (RandomAccessFile file = new RandomAccessFile("example.txt", "rw");
             FileChannel channel = file.getChannel()) {

            // 获取对该文件的独占锁定
            FileLock lock = channel.lock();
            try {
                // 在这个区域进行文件操作
                System.out.println("File is locked for exclusive access");
            } finally {
                lock.release(); // 释放锁
                System.out.println("Lock released");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5. 对象序列化

对象序列化是将对象的状态转换为可以存储或传输的格式的过程。在Java中,这通常意味着转换为字节流。

import java.io.*;

public class SerializationExample {
    private static class Student implements Serializable {
        private static final long serialVersionUID = 1L;
        String name;
        transient int age; // 使用 transient 关键字标记非序列化字段

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{name='" + name + "', age=" + age + "}";
        }
    }

    public static void main(String[] args) {
        Student student = new Student("John Doe", 20);

        // 序列化
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.ser"))) {
            out.writeObject(student);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.ser"))) {
            Student deserializedStudent = (Student) in.readObject();
            System.out.println(deserializedStudent);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

6. Java NIO 2 (File API)

Java 7 引入了 NIO 2,它在原有 NIO 的基础上增加了对文件处理和文件系统特性的支持。

import java.nio.file.*;

public class NIO2Example {
    public static void main(String[] args) {
        Path path = Paths.get("somefile.txt");

        try {
            // 创建文件
            if (!Files.exists(path)) {
                Files.createFile(path);
            }

            // 写入内容到文件
            Files.write(path, "Hello, World!".getBytes(), StandardOpenOption.APPEND);

            // 读取文件内容
            byte[] bytes = Files.readAllBytes(path);
            System.out.println(new String(bytes));

            // 删除文件
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

面试相关问题

1. 解释Java IO中的流(Stream)是什么。

在Java中,流(Stream)是一种抽象概念,用于在程序和数据源或目标之间顺序读写数据。它们可以来自或去向不同的数据源,如文件、数组、网络连接等。流通常分为输入流和输出流,其中输入流用于从数据源读取数据,输出流用于向目标写数据。Java IO库提供了多种流类型,包括字节流(如InputStream和OutputStream)和字符流(如Reader和Writer)。

2. 字节流和字符流有什么区别?

字节流和字符流是Java IO中处理输入和输出的两种主要方式。字节流(如InputStream和OutputStream)以字节为单位处理数据,适用于处理所有类型的数据,包括二进制数据、图像、视频文件等。字符流(如Reader和Writer)以字符为单位处理数据,专门用于处理文本数据。使用字符流读写时,Java虚拟机会自动处理和转换字符到字节之间的转换。

3. 什么是缓冲流,使用它有什么好处?

缓冲流(Buffered Streams)是Java IO中的一种包装流,用于提高读写数据的效率。它通过设置内部缓冲区来减少实际读写硬盘的次数。例如,BufferedReader和BufferedWriter分别用于包装其他的字符输入和输出流,使得单个字符的读写操作转变为批量读写,大大提高了IO性能。使用缓冲流的好处主要是提高程序的执行效率,减少与磁盘交互的次数。

4. Java中如何实现文件的复制?

在Java中实现文件复制通常使用字节流或字符流,具体取决于文件的类型(二进制或文本)。

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

public class FileCopy {
    public static void main(String[] args) {
        try (FileInputStream in = new FileInputStream("source.txt");
             FileOutputStream out = new FileOutputStream("destination.txt")) {
            int byteRead;
            while ((byteRead = in.read()) != -1) {
                out.write(byteRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5. 解释序列化和反序列化。

序列化是指将对象的状态转换为可以存储或传输的格式(如JSON、XML或二进制格式)的过程,以便可以在文件系统、数据库或网络中保存或发送。反序列化是将已序列化的数据恢复为原始对象的过程。在Java中,可以通过实现Serializable接口,并使用ObjectOutputStream和ObjectInputStream来进行对象的序列化和反序列化。这使得对象可以通过网络发送或持久化存储到磁盘上。

总体知识

1. 高级文件处理

随机访问文件:使用RandomAccessFile类进行文件的随机访问。这允许你跳到文件的任何位置读写数据,非常适合需要频繁查找的大文件。
文件加锁:理解如何对文件片段使用锁定机制,防止并发访问时的数据冲突。这可以通过FileChannel的lock()方法实现。

2. Java NIO

Buffers and Channels:理解缓冲区(Buffers)和通道(Channels)的使用,这是NIO中数据处理的核心。掌握不同类型的Buffer如ByteBuffer、CharBuffer等,以及它们的使用场景。
非阻塞IO:了解如何使用非阻塞IO进行网络通信,这包括设置非阻塞模式的SocketChannel和ServerSocketChannel。
选择器(Selectors):掌握使用选择器管理多个通道的技术。选择器允许单线程处理多个Channel的IO操作,适合实现高性能的网络服务器。

3. 文件系统操作(Java NIO.2)

文件和目录的操作:使用Files和Path类来执行复杂的文件系统操作,如文件属性读取、文件树的遍历、创建符号链接等。
文件变更监听:使用WatchService API 监听文件系统的变化,对于需要实时响应文件修改的应用非常有用。

4. 序列化机制

自定义序列化:了解如何自定义序列化过程,包括使用transient关键字处理不需要序列化的属性,以及实现Externalizable接口进行更精细控制的序列化和反序列化。
序列化的安全性:理解和防范序列化过程中可能的安全风险,如防止序列化数据的篡改和重放攻击。

5. 性能优化

IO性能调优:理解和应用各种IO优化技术,比如合理使用缓冲、选择合适的读写方式和大小、以及利用现代操作系统的高级特性如内存映射(Memory-Mapped Files)。
资源管理:在IO操作中采用正确的资源管理策略,例如使用try-with-resources语句确保及时释放资源。

6. 最佳实践

异常处理:精确地处理IO操作中可能发生的异常,避免因为资源泄露或数据不一致导致的问题。
数据完整性:在进行文件读写操作时,确保数据的完整性和一致性,尤其是在并发场景下。

使用NIO进行文件复制

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class FileCopyNIO {
    public static void copyFile(Path source, Path target) {
        try (FileChannel srcChannel = FileChannel.open(source, StandardOpenOption.READ);
             FileChannel destChannel = FileChannel.open(target, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
            
            srcChannel.transferTo(0, srcChannel.size(), destChannel);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Path sourcePath = Path.of("source.txt");
        Path targetPath = Path.of("target.txt");
        copyFile(sourcePath, targetPath);
    }
}
  • 14
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CopyLower

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

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

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

打赏作者

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

抵扣说明:

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

余额充值