
本文旨在指导开发者如何在 Java 中使用 FileInputStream 按字节读取文件,并解决可能遇到的编码问题。文章将详细讲解如何正确读取文件,以及如何在字节流转换为字符串时指定正确的编码方式,从而确保数据的准确性和一致性。
在 Java 中,FileInputStream 类是用于从文件中读取原始字节流的核心类。它允许你以字节为单位读取文件内容,这在处理二进制文件或需要精确控制读取过程时非常有用。
基本用法:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String[] args) {
String filePath = "test.tpf"; // 替换为你的文件路径
try (FileInputStream fis = new FileInputStream(filePath)) {
int byteData;
while ((byteData = fis.read()) != -1) {
// 处理读取到的字节
System.out.print((char) byteData); // 简单示例:将字节转换为字符并打印
}
} catch (IOException e) {
e.printStackTrace();
}
}
}按指定大小读取:
立即学习“Java免费学习笔记(深入)”;
如果你需要按固定大小的块读取文件,例如 16 字节,可以这样做:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamBlockRead {
public static void main(String[] args) {
String filePath = "test.tpf"; // 替换为你的文件路径
int blockSize = 16;
byte[] buffer = new byte[blockSize];
try (FileInputStream fis = new FileInputStream(filePath)) {
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
// 处理读取到的字节块
// bytesRead 表示实际读取到的字节数,可能小于 blockSize
for (int i = 0; i < bytesRead; i++) {
System.out.print((char) buffer[i]); // 简单示例:将字节转换为字符并打印
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}当使用 FileInputStream 读取文件后,如果需要将字节数据转换为字符串,编码就变得至关重要。 FileInputStream 本身不涉及编码,编码发生在将字节数组转换为字符串的步骤。如果文件使用非系统默认编码,需要明确指定编码方式。
指定编码方式:
可以使用 String 类的构造函数,指定编码方式将字节数组转换为字符串:
String decodedString = new String(byteArray, "MS949"); // 使用 MS949 编码
示例:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamEncoding {
public static void main(String[] args) {
String filePath = "test.tpf"; // 替换为你的文件路径
int blockSize = 16;
byte[] buffer = new byte[blockSize];
String encoding = "MS949"; // 替换为你的文件编码
try (FileInputStream fis = new FileInputStream(filePath)) {
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
// 将字节数组转换为字符串,并指定编码
String decodedString = new String(buffer, 0, bytesRead, encoding);
System.out.print(decodedString);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}注意事项:
根据提供的代码片段,问题可能出在以下几个方面:
解密后的字节数组大小: seed.SeedDecrypt(fileContentArray, pdwRoundKey, outbuf); 假设 SeedDecrypt 方法总是填充 outbuf 的全部 16 字节。如果解密后的实际有效数据小于 16 字节,则 outbuf 中可能包含填充数据,导致后续处理出现问题。
字符串拼接: 循环中使用 mergeStr += dbgmsg; 拼接字符串效率较低。推荐使用 StringBuilder 或 StringBuffer 来提高性能。
Base64 编码: 在对 imageData 进行 Base64 编码之前,需要确保 imageData 的内容是正确的。如果 imageData 包含乱码或错误数据,Base64 编码的结果也会出错。
改进后的代码示例:
import java.io.*;
import java.util.Base64;
public class FileDecryptExample {
public static void main(String[] args) {
String tpf = "test.tpf"; // 替换为你的文件路径
String encoding = "MS949"; // 替换为你的文件编码
// ... (pdwRoundKey 和 seed 的初始化)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int nFileSize = 0; // 替换为你的文件大小
int nReadCur = 0;
byte[] outbuf = new byte[16];
int nRead;
try (FileInputStream fis = new FileInputStream(tpf)) {
while (true) {
byte[] fileContentArray = new byte[16];
nRead = fis.read(fileContentArray);
if (nRead == -1) {
break; // 文件读取完毕
}
// 确保只解密实际读取到的字节
byte[] decryptedBytes = new byte[16];
seed.SeedDecrypt(fileContentArray, pdwRoundKey, decryptedBytes); // 解密
baos.write(decryptedBytes, 0, nRead); // 将解密后的字节写入 ByteArrayOutputStream
nReadCur += nRead;
if (nFileSize <= nReadCur) {
break;
}
}
String mergeStr = new String(baos.toByteArray(), encoding);
String[] dataExplode = mergeStr.split("<TextData>");
String[] dataExplode1 = dataExplode[1].split("</FileInfo>");
String[] dataExplode2 = dataExplode1[0].split("</TextData>");
String textData = dataExplode2[0];
String imageData = dataExplode1[1];
Base64.Encoder encoder = Base64.getEncoder();
imageData = encoder.encodeToString(imageData.getBytes(encoding)); // 指定编码
// ... (构建 JSONArray 和 result)
System.out.println("mergeStr:" + mergeStr.length() + " / image:" + imageData.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}改进说明:
本文详细介绍了如何在 Java 中使用 FileInputStream 按字节读取文件,并处理可能遇到的编码问题。 核心在于理解 FileInputStream 的字节流特性,以及在将字节流转换为字符串时正确指定编码方式。 通过本文的学习,你应该能够更自信地处理 Java 中的文件读取和编码转换问题。 在实际应用中,请务必根据文件的实际编码选择正确的编码方式,并进行充分的测试,以确保数据的准确性和一致性。
以上就是Java 中按字节读取文件并处理编码问题的实践指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号