【发布时间】:2026-02-04 14:05:01
【问题描述】:
我需要检查文件中的每个字符并将其转换为字节。但不幸的是扫描仪没有提供任何不分割行的最后一个字符的可能性...... 我尝试做这样的事情:
Scanner in = new Scanner(new File(path));
List<Byte> byteList = new ArrayList<>();
while (in.hasNextLine()) {
String a = in.nextLine();
if (in.hasNextLine()) {
a = a + (char) (13);
}
for (char c : a.toCharArray()) {
byteList.add((byte) c);
}
}
byte[] bytes = new byte[byteList.size()];
for (int i = 0; i < byteList.size(); i++) {
bytes[i] = byteList.get(i);
}
return bytes;
}
您对这个问题的解决方案有任何想法吗? 我会很感激你的帮助。
【问题讨论】:
-
所以不要使用扫描仪,使用 FileInputStream。