我正在尝试读取一个可以在here中找到的短代码文件二进制文件。
我用来打印这个文件内容的方法如下:
public void read3RegularGraphs( String pathFile ) throws IOException {
InputStream reader = new FileInputStream(pathFile);
byte [] fileBytes = Files.readAllBytes(new File(pathFile).toPath());
char singleChar;
for(byte b : fileBytes) {
singleChar = (char) b;
System.out.print(singleChar);
}
}不幸的是,我得到的输出格式不正确,我得到的是符号而不是字符。
如何将二进制内容转换为字符格式?
谢谢
发布于 2019-05-06 11:38:33
使用解码时需要传入字符集。Char和byte是两码事
List<String> stringList = Files.readAllLines(new File(pathFile).toPath(), Charset.forName("UTF-8"));在Char、字符串和字节数组之间进行转换时,请显式声明字符集
byte[] byteArray= stringTest.getBytes(Charset.forName("UTF-8"));
String stringTest = new String(byteArray, Charset.forName("UTF-8"));https://stackoverflow.com/questions/55998397
复制相似问题