Java I/O操作

ops/2024/10/18 12:27:10/

java-io操作" style="background-color:transparent;">引言

在Java编程中,输入和输出(I/O)操作是必不可少的部分。Java I/O通过一系列流(Stream)类和方法,支持文件操作、控制台输入输出、网络I/O等多种I/O操作。本文将详细介绍Java I/O的基础概念、文件操作、字符流与字节流的区别、序列化与反序列化以及新的I/O(NIO)等内容,并通过表格进行总结和示范。

java-io基础概念">Java I/O基础概念

流的概念

流(Stream)是指数据的流动,是一个顺序读写数据的抽象。Java定义了两种基本类型的流:

  • 字节流:用于处理字节数据,最基础的类是InputStreamOutputStream
  • 字符流:用于处理字符数据,最基础的类是ReaderWriter

流的分类

Java I/O流可以按照数据类型分类为字节流和字符流,也可以按照数据流向分为输入流和输出流。

类型输入流输出流
字节流InputStreamOutputStream
字符流ReaderWriter

文件与目录操作

文件类(File)

File类表示文件或目录,可以对其进行创建、删除、获取信息等操作。

java">import java.io.File;
import java.io.IOException;public class FileExample {public static void main(String[] args) {// 创建一个File对象File file = new File("example.txt");try {// 创建新文件if (file.createNewFile()) {System.out.println("File created: " + file.getName());} else {System.out.println("File already exists.");}// 获取文件信息System.out.println("Absolute path: " + file.getAbsolutePath());System.out.println("Writable: " + file.canWrite());System.out.println("Readable: " + file.canRead());System.out.println("File size in bytes: " + file.length());} catch (IOException e) {System.out.println("An error occurred.");e.printStackTrace();}}
}

文件与目录操作方法表

方法描述示例
createNewFile()创建一个新的文件file.createNewFile();
delete()删除文件或目录file.delete();
exists()判断文件或目录是否存在file.exists();
getAbsolutePath()获取文件的绝对路径file.getAbsolutePath();
canWrite()判断文件是否可写file.canWrite();
canRead()判断文件是否可读file.canRead();
length()获取文件大小(字节)file.length();

字符流和字节流

字节流

字节流用于处理字节数据,通过InputStreamOutputStream类及其子类进行输入输出操作。

示例:使用FileInputStreamFileOutputStream进行文件字节操作

java">import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class ByteStreamExample {public static void main(String[] args) {try (FileInputStream fis = new FileInputStream("input.txt");FileOutputStream fos = new FileOutputStream("output.txt")) {int byteData;// 逐字节读取数据while ((byteData = fis.read()) != -1) {// 逐字节写入数据fos.write(byteData);}} catch (IOException e) {e.printStackTrace();}}
}

字符流

字符流用于处理字符数据,通过ReaderWriter类及其子类进行输入输出操作。

示例:使用FileReaderFileWriter进行文件字符操作

java">import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CharStreamExample {public static void main(String[] args) {try (FileReader fr = new FileReader("input.txt");FileWriter fw = new FileWriter("output.txt")) {int charData;// 逐字符读取数据while ((charData = fr.read()) != -1) {// 逐字符写入数据fw.write(charData);}} catch (IOException e) {e.printStackTrace();}}
}

字符流和字节流的比较表

特性字节流字符流
基本类InputStream 和 OutputStreamReader 和 Writer
数据处理单位字节(8位)字符(16位)
适用场景二进制数据(如图像、音频)文本数据(如文本文件)
典型实现类FileInputStreamFileOutputStreamFileReaderFileWriter

序列化与反序列化

序列化

序列化是将对象转换为字节流并写到文件或网络中的过程。实现Serializable接口的类可以被序列化。

示例:对象序列化

java">import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;class Person implements Serializable {private static final long serialVersionUID = 1L;String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}
}public class SerializationExample {public static void main(String[] args) {Person person = new Person("John", 30);try (FileOutputStream fos = new FileOutputStream("person.ser");ObjectOutputStream oos = new ObjectOutputStream(fos)) {oos.writeObject(person);} catch (IOException e) {e.printStackTrace();}}
}

反序列化

反序列化是将字节流转换为对象的过程。读取序列化文件时使用ObjectInputStream

示例:对象反序列化

java">import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;public class DeserializationExample {public static void main(String[] args) {try (FileInputStream fis = new FileInputStream("person.ser");ObjectInputStream ois = new ObjectInputStream(fis)) {Person person = (Person) ois.readObject();System.out.println("Name: " + person.name);System.out.println("Age: " + person.age);} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}
}

序列化与反序列化的表格总结

概念描述示例
序列化将对象转换为字节流并写到文件或网络中ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.ser")); oos.writeObject(object);
反序列化将字节流转换为对象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.ser")); Object obj = ois.readObject();
必需接口需要实现 Serializable 接口class Person implements Serializable { ... }
序列化ID建议定义 serialVersionUID 字段应避免反序列化不兼容private static final long serialVersionUID = 1L;

新的I/O (NIO)

Java的新的I/O(NIO)引入了多个新概念,如缓冲区、通道和选择器。这些概念提供了更高效的I/O处理,特别是在处理大数据和高性能网络应用方面。下面我们将详细介绍这些概念。

缓冲区(Buffer)

缓冲区是NIO数据操作的基础,用于存储和操作数据。Java NIO提供了多种类型的缓冲区,比如ByteBufferCharBuffer等。

示例:使用ByteBuffer

java">import java.nio.ByteBuffer;public class BufferExample {public static void main(String[] args) {// 创建一个大小为10的字节缓冲区ByteBuffer buffer = ByteBuffer.allocate(10);// 写入数据到缓冲区buffer.put((byte) 1);buffer.put((byte) 2);// 切换到读模式buffer.flip();// 读取缓冲区中的数据while (buffer.hasRemaining()) {byte data = buffer.get();System.out.println(data);}}
}

通道(Channel)

通道是一个连接数据源和缓冲区的通道,数据可以通过通道从数据源读入缓冲区,或从缓冲区写入到数据源。常用的通道有FileChannelSocketChannelServerSocketChannel等。

示例:使用FileChannel

java">import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;public class ChannelExample {public static void main(String[] args) {// 文件路径Path path = Path.of("example.txt");// 使用文件通道写入数据try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {ByteBuffer buffer = ByteBuffer.allocate(64);buffer.put("Hello, NIO!".getBytes());buffer.flip();fileChannel.write(buffer);} catch (IOException e) {e.printStackTrace();}// 使用文件通道读取数据try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ)) {ByteBuffer buffer = ByteBuffer.allocate(64);fileChannel.read(buffer);buffer.flip();while (buffer.hasRemaining()) {System.out.print((char) buffer.get());}} catch (IOException e) {e.printStackTrace();}}
}

选择器(Selector)

选择器是NIO中的多路复用器,可以用于管理多个通道,特别适用于网络编程。SelectorSelectableChannel配合使用,可以高效地处理非阻塞I/O操作。

示例:使用Selector进行非阻塞I/O

java">import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;public class SelectorExample {public static void main(String[] args) {try {// 打开选择器Selector selector = Selector.open();// 打开服务器套接字通道ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.socket().bind(new InetSocketAddress(8080));serverSocketChannel.configureBlocking(false);// 将通道注册到选择器,并监听接受事件serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);// 循环等待事件while (true) {selector.select();Set<SelectionKey> selectedKeys = selector.selectedKeys();Iterator<SelectionKey> iterator = selectedKeys.iterator();while (iterator.hasNext()) {SelectionKey key = iterator.next();iterator.remove();if (key.isAcceptable()) {// 接受新连接ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();SocketChannel socketChannel = serverChannel.accept();socketChannel.configureBlocking(false);socketChannel.register(selector, SelectionKey.OP_READ);} else if (key.isReadable()) {// 读取数据SocketChannel socketChannel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(256);socketChannel.read(buffer);buffer.flip();System.out.println("Received: " + new String(buffer.array()).trim());}}}} catch (IOException e) {e.printStackTrace();}}
}

NIO关键概念表格总结

概念描述示例
缓冲区(Buffer)用于存储数据的容器,可以是字节、字符、短整型等ByteBuffer buffer = ByteBuffer.allocate(10);
通道(Channel)用于连接数据源与缓冲区,实现I/O操作FileChannel.open(path, StandardOpenOption.READ);
选择器(Selector)用于管理多个通道,实现非阻塞I/O的多路复用Selector.open(); selector.select();

表格总结

文件与目录操作方法表

方法描述示例
createNewFile()创建一个新的文件file.createNewFile();
delete()删除文件或目录file.delete();
exists()判断文件或目录是否存在file.exists();
getAbsolutePath()获取文件的绝对路径file.getAbsolutePath();
canWrite()判断文件是否可写file.canWrite();
canRead()判断文件是否可读file.canRead();
length()获取文件大小(字节)file.length();

字符流和字节流的比较表

特性字节流字符流
基本类InputStream 和 OutputStreamReader 和 Writer
数据处理单位字节(8位)字符(16位)
适用场景二进制数据(如图像、音频)文本数据(如文本文件)
典型实现类FileInputStreamFileOutputStreamFileReaderFileWriter

序列化与反序列化的表格总结

概念描述示例
序列化将对象转换为字节流并写到文件或网络中ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.ser")); oos.writeObject(object);
反序列化将字节流转换为对象ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.ser")); Object obj = ois.readObject();
必需接口需要实现 Serializable 接口class Person implements Serializable { ... }
序列化ID建议定义 serialVersionUID 字段以避免反序列化不兼容private static final long serialVersionUID = 1L;

NIO关键概念表格总结

概念描述示例
缓冲区(Buffer)用于存储数据的容器,可以是字节、字符、短整型等ByteBuffer buffer = ByteBuffer.allocate(10);
通道(Channel)用于连接数据源与缓冲区,实现I/O操作FileChannel.open(path, StandardOpenOption.READ);
选择器(Selector)用于管理多个通道,实现非阻塞I/O的多路复用Selector.open(); selector.select();

总结

本文详细介绍了Java I/O操作,包括文件和目录操作、字符流与字节流的区别、序列化与反序列化、新的I/O(NIO)等内容。通过示例代码和表格总结,帮助您更好地理解和应用Java中的I/O操作,提高程序的读取和写入效率。


http://www.ppmy.cn/ops/50036.html

相关文章

解决:AttributeError: module ‘tensorflow‘ has no attribute ‘***‘

问题产生的原因是当前Python使用的Tensorflow库为2.0最新版本&#xff0c;而源代码使用的是1.0版本&#xff0c;在不降低版本的情况下运行代码需要做些调整&#xff1a; 找到报错的地方&#xff0c;在报错的attribute前面加上compat.v1. 举例说明&#xff1a; 源码&#xff…

.NETCORE 微软企业登录

1.常用类如下 Samlv1ClaimsTransform.cs public static class Samlv1ClaimsTransform {public static ClaimsPrincipal Transform(ClaimsPrincipal incomingPrincipal){if (!incomingPrincipal.Identity.IsAuthenticated){return incomingPrincipal;}return CreateClaimsPrin…

【html5的video标签在移动端的使用】【微信内部浏览器video自动播放】【vue-video-player】

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、使用步骤1. html部分2.js部分 二、使用插件vue-video-player1、下载插件2、使用3、在组件中使用 三、video相关文章推荐 前言 在移动端的首页用视频做背景…

果园预售系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;管理员管理&#xff0c;用户管理&#xff0c;果树管理&#xff0c;果园管理&#xff0c;果园预约管理 前台账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;论坛&#xff0c;公告&a…

Mysql 的分布式策略

1. 前言 MySQL 作为最最常用的数据库&#xff0c;了解 Mysql 的分布式策略对于掌握 MySQL 的高性能使用方法和更安全的储存方式有非常重要的作用。 它同时也是面试中最最常问的考点&#xff0c;我们这里就简单总结下 Mysq 的常用分布式策略。 2. 复制 复制主要有主主复制和…

描述React中的函数组件和类组件之间的区别

React中的函数组件和类组件之间存在显著的区别&#xff0c;以下是对这些区别的详细描述&#xff1a; 语法与设计思想&#xff1a; 函数组件&#xff1a;采用函数式编程思想&#xff0c;使用纯JavaScript函数定义。函数组件接收一个输入参数props&#xff0c;并返回一个React元…

四川古力未来科技抖音小店打造品质生活,可靠之选引领潮流

在当今数字化快速发展的时代&#xff0c;电商平台如雨后春笋般涌现&#xff0c;抖音小店作为其中的佼佼者&#xff0c;凭借其独特的短视频电商模式&#xff0c;迅速吸引了大批年轻消费者的目光。而在众多的抖音小店中&#xff0c;四川古力未来科技抖音小店凭借其卓越的品质和专…

SylixOS下UDP组播测试程序

SylixOS下UDP组播测试 测试效果截图如下: udp组播发送测试程序。 /********************************************************************************************************* ** ** 中国软件开源组织 ** ** …