Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用新行字符加载文件_Java - Fatal编程技术网

Java 用新行字符加载文件

Java 用新行字符加载文件,java,Java,以下是我的代码,以字节形式加载内容,然后将整数转换为十六进制值: try { InputStream is = new FileInputStream(file); while ((byteRead = is.read()) != -1) { //System.out.print(((char) byteRead).charAt(0)); String temp = Character.toString((char)

以下是我的代码,以字节形式加载内容,然后将整数转换为十六进制值:

    try {
    InputStream is = new FileInputStream(file);
        while ((byteRead = is.read()) != -1) {
            //System.out.print(((char) byteRead).charAt(0));
            String temp = Character.toString((char) byteRead);
            ta.append(Character.toString((char) byteRead));
            System.out.print(Character.toString((char) byteRead));

            ta2.append(Integer.toHexString(byteRead) + " ");

        }
    }
输出:414243da313233

但看起来,新行字符无法读取,我知道新行字符的十六进制值是“0A”,如何修复它

否,您正在读取换行符-即“d”后跟“a”是-回车符后跟换行符(“\r\n”)

但是,您当前假定该文件位于ISO-8859-1中。要读取文本文件,您几乎应该始终使用
读取器
,而不是直接从流中读取字节。我建议使用
InputStreamReader
,明确指定编码。例如:

InputStreamReader reader = new InputStreamReader(
    new FileInputStream(file), "UTF-8");

您可能还希望将读取器包装在一个
BufferedReader
中,这样可以方便地读取行。或者,我可以彻底推荐中的实用程序类(尤其是),它使处理类似文件更容易。

但是如何更改文件强制输出打印“0A”?@hk访谈:这与读取文件无关-这是字节值10到十六进制的转换。这就是你真正的问题所在:通过填充将字节转换为十六进制?