gpt4 book ai didi

java - Reg - 从字节流中读取的数据

转载 作者:行者123 更新时间:2023-11-30 11:09:08 24 4
gpt4 key购买 nike

我正在尝试使用字节流读取包含普通文本数据的文件。而且我知道在字节流中,每个字节都将被一个一个地读取。因此,如果我通过字节流读取文本文件中的数据 Hi How are you!!!!! 那么它应该给我每个字符的 Unicode 等价物,但它给了我一个不映射到 utf 或 ascii 等价物的不同输出。

下面是我的程序

package files;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileBaiscs {
public static void main(String[] args) throws IOException {
String strPath = "D:\\Files\\New.txt";
File objSrcFile = new File (strPath);
if (objSrcFile.exists()==false)
{
System.out.println("The Source File is not Present");
System.exit(0);
}
FileInputStream objIpStream = new FileInputStream(objSrcFile);
while ((objIpStream.read())!=-1)
{
System.out.println(objIpStream.read());
}
objIpStream.close();
}
}

我控制台的输出是:

105
72
119
97
101
121
117
33
33
33

新文本文件中的数据是 - Hi How are you!!!!!!

我希望输出是与每个字符上面的 utf 等效的整数。如果我的理解有误,请告诉我。

最佳答案

这里

  while ((objIpStream.read())!=-1)
{
System.out.println(objIpStream.read());
}

您正在读取 2 个字节而不是一个字节。第一个在条件中读取,第二个在循环体中读取。你应该做的是

byte b;
while ((b=objIpStream.read())!=-1)
{
System.out.println(b);
}

关于java - Reg - 从字节流中读取的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com