Java IO(Input/Output)流是用于处理输入和输出数据的机制。它提供了一种标准化的方式来读取和写入数据,可以与文件、网络、标准输入输出等进行交互。
Java IO流主要分为两个流模型:字节流(Byte Stream)和字符流(Character Stream)。
字节流(Byte Stream)
常见的字节流实现类包括:
字符流(Character Stream)
常见的字符流实现类包括:
使用Java IO流的一般步骤如下:
示例
// 需要注意的是,在使用流操作完成后,我们应该及时关闭流以释放资源,以上述代码为例,使用了try-with-resources语句来实现自动关闭流。
try (FileInputStream fis = new FileInputStream("input.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("output.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
Java NIO(New IO)是Java 1.4版本引入的一套新的IO API,提供了更高效、更灵活的IO操作方式。
相比于传统的Java IO(InputStream/OutputStream),Java NIO主要有以下几个特点:
使用Java NIO进行IO操作的一般步骤如下:
示例
try (RandomAccessFile file = new RandomAccessFile("file.txt", "rw");
FileChannel channel = file.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();
}
Java NIO提供了更高效、更灵活的IO处理方式,适用于需要处理大量连接或需要高性能和低延迟的场景。相比于传统的Java IO,它可以更好地满足现代应用对IO操作的需求。但需要注意的是,Java NIO的API较为复杂,使用时需要仔细理解和学习。
choco install ffmpeg
choco install ffmpeg-full
package java_io_nio;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FileUtils {
/**
* 拷贝文件 FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream 应用
* @param intputFilePath 输入文件
* @param outputFilePath 输出文件
*/
public static void copyFileByPath(String intputFilePath, String outputFilePath){
try(FileInputStream fis = new FileInputStream(intputFilePath);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(outputFilePath, false);
BufferedOutputStream bos = new BufferedOutputStream(fos)){
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1){
bos.write(buffer, 0, bytesRead);
}
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取文件内容 BufferedReader 应用
* @param filePath 文件路径
* @return
*/
public static String readFromFile(String filePath){
StringBuffer result = new StringBuffer();
try (FileInputStream fr = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fr, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
String line;
while((line = br.readLine()) != null){
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
/**
* 写入文件 BufferedWriter 应用
* @param filePath 文件路径
* @param content 文件内容
*/
public static void writeToFile(String filePath, String content){
try (FileOutputStream fos = new FileOutputStream(filePath);
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(osw)) {
bw.write(content);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件拷贝 NIO 应用
* @param sourcePath 源文件
* @param targetPath 目标文件
*/
public static void transferFile(String sourcePath, String targetPath){
Path sPath = Paths.get(sourcePath);
Path tPath = Paths.get(targetPath);
try {
Files.copy(sPath, tPath, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 视频格式转换 NIO 应用
* @param inputPath 源格式
* @param outputPath 目标格式
*/
public static void videoFormatConversion(String inputPath, String outputPath){
try {
// 输入视频文件路径
Path iPath = Paths.get(inputPath);
// 输出转换后的视频文件路径
Path oPath = Paths.get(outputPath);
// 构建 FFmpeg 命令
String ffmpegCommand = "ffmpeg -i " + iPath.toString() + " -c:v copy -c:a copy " + oPath.toString();
// 执行 FFmpeg 命令
Process process = Runtime.getRuntime().exec(ffmpegCommand);
process.waitFor();
// 检查转换后的文件是否存在
if (Files.exists(oPath)) {
System.out.println("视频格式转换成功!");
} else {
System.out.println("视频格式转换失败!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
/**
* 读取数据 NIO 应用
* @param socketChannel
*/
private static void handleClientRequest(SocketChannel socketChannel) {
try {
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = socketChannel.read(buffer);
while (bytesRead != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
bytesRead = socketChannel.read(buffer);
}
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
copyFileByPath("D:\\project\\uploadPath\\file_0.png", "D:\\project\\uploadPath\\file_1.png");
String fileContent = readFromFile("D:\\project\\uploadPath\\test.txt");
System.out.println("文件内容:" + fileContent);
writeToFile("D:\\project\\uploadPath\\test.txt", "测试内容");
transferFile("D:\\project\\uploadPath\\file_0.png", "D:\\project\\uploadPath\\file_2.png");
videoFormatConversion("D:\\project\\uploadPath\\file_0.mp4", "D:\\project\\uploadPath\\file_0.mov");
// NIO 网络编程
try {
// 创建 ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
serverSocketChannel.configureBlocking(false);
System.out.println("服务器已启动,监听端口 8080...");
// 创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
while (true) {
// 监听客户端连接
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
System.out.println("客户端连接成功!");
// 处理客户端请求的任务提交给线程池执行
executorService.submit(() -> handleClientRequest(socketChannel));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ffmpeg 下载