如何拆分包含多个 "lines" in Java 的字节数组?
How to split a byte array that contains multiple "lines" in Java?
假设我们有这样一个文件:
one
two
three
(但此文件已加密)
我的加密方法returns内存中的整个文件,作为 byte[] 类型。
我知道字节数组没有 "lines" 的概念,这是 Scanner(例如)可能有的概念。
我想遍历每一行,将其转换为字符串并对其进行操作,但我不知道
如何:
- 在字节数组中查找行
- 将原始字节数组切片为 "lines"(我会将这些切片转换为字符串,以发送到我的其他方法)
- 正确遍历一个字节数组,每次迭代都是一个新的"line"
另外:我是否需要考虑不同的 OS 文件可能已经组成?我知道 Windows 和 Linux 中的新行之间存在一些差异,我不希望我的方法仅适用于一种格式。
编辑:根据此处答案中的一些提示,我能够编写一些代码来完成工作。我仍然想知道这段代码是否值得保留,或者我正在做一些将来可能会失败的事情:
byte[] decryptedBytes = doMyCrypto(fileName, accessKey);
ByteArrayInputStream byteArrInStrm = new ByteArrayInputStream(decryptedBytes);
InputStreamReader inStrmReader = new InputStreamReader(byteArrInStrm);
BufferedReader buffReader = new BufferedReader(inStrmReader);
String delimRegex = ",";
String line;
String[] values = null;
while ((line = buffReader.readLine()) != null) {
values = line.split(delimRegex);
if (Objects.equals(values[0], tableKey)) {
return values;
}
}
System.out.println(String.format("No entry with key %s in %s", tableKey, fileName));
return values;
特别是,有人建议我明确设置编码,但我看不到具体位置?
正如 Scott 所说,我想看看你想出了什么,这样我们就可以帮助你改变它以满足你的需要。
关于您对 OS 的最后评论;如果你想支持多种文件类型,你应该考虑制作几个支持这些不同文件扩展名的函数。据我所知,您确实需要指定您使用代码读取的文件和文件类型。
如果你想直播这个,我建议:
- 创建一个
ByteArrayInputStream
来包装您的数组
- 将其包装在
InputStreamReader
中以将二进制数据转换为文本 - 我建议您明确指定所使用的文本编码
- 围绕它创建一个
BufferedReader
以一次读取一行
那么你可以使用:
String line;
while ((line = bufferedReader.readLine()) != null)
{
// Do something with the line
}
BufferedReader
处理所有操作系统的换行符。
所以像这样:
byte[] data = ...;
ByteArrayInputStream stream = new ByteArrayInputStream(data);
InputStreamReader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
请注意,通常您希望对流和读取器使用 try-with-resources 块 - 但在这种情况下并不重要,因为它只是在记忆中。
假设我们有这样一个文件:
one
two
three
(但此文件已加密)
我的加密方法returns内存中的整个文件,作为 byte[] 类型。
我知道字节数组没有 "lines" 的概念,这是 Scanner(例如)可能有的概念。
我想遍历每一行,将其转换为字符串并对其进行操作,但我不知道 如何:
- 在字节数组中查找行
- 将原始字节数组切片为 "lines"(我会将这些切片转换为字符串,以发送到我的其他方法)
- 正确遍历一个字节数组,每次迭代都是一个新的"line"
另外:我是否需要考虑不同的 OS 文件可能已经组成?我知道 Windows 和 Linux 中的新行之间存在一些差异,我不希望我的方法仅适用于一种格式。
编辑:根据此处答案中的一些提示,我能够编写一些代码来完成工作。我仍然想知道这段代码是否值得保留,或者我正在做一些将来可能会失败的事情:
byte[] decryptedBytes = doMyCrypto(fileName, accessKey);
ByteArrayInputStream byteArrInStrm = new ByteArrayInputStream(decryptedBytes);
InputStreamReader inStrmReader = new InputStreamReader(byteArrInStrm);
BufferedReader buffReader = new BufferedReader(inStrmReader);
String delimRegex = ",";
String line;
String[] values = null;
while ((line = buffReader.readLine()) != null) {
values = line.split(delimRegex);
if (Objects.equals(values[0], tableKey)) {
return values;
}
}
System.out.println(String.format("No entry with key %s in %s", tableKey, fileName));
return values;
特别是,有人建议我明确设置编码,但我看不到具体位置?
正如 Scott 所说,我想看看你想出了什么,这样我们就可以帮助你改变它以满足你的需要。
关于您对 OS 的最后评论;如果你想支持多种文件类型,你应该考虑制作几个支持这些不同文件扩展名的函数。据我所知,您确实需要指定您使用代码读取的文件和文件类型。
如果你想直播这个,我建议:
- 创建一个
ByteArrayInputStream
来包装您的数组 - 将其包装在
InputStreamReader
中以将二进制数据转换为文本 - 我建议您明确指定所使用的文本编码 - 围绕它创建一个
BufferedReader
以一次读取一行
那么你可以使用:
String line;
while ((line = bufferedReader.readLine()) != null)
{
// Do something with the line
}
BufferedReader
处理所有操作系统的换行符。
所以像这样:
byte[] data = ...;
ByteArrayInputStream stream = new ByteArrayInputStream(data);
InputStreamReader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
请注意,通常您希望对流和读取器使用 try-with-resources 块 - 但在这种情况下并不重要,因为它只是在记忆中。