简体   繁体   English

java utf-8文件读取

[英]java utf-8 file reading

I am trying to read a UTF-8 encoded file as follows- 我正在尝试读取UTF-8编码的文件,如下所示:

import java.io.*;

class main {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("temp.txt"), "UTF-8"));
        String line;
        line = br.readLine();
        line = line.trim();
        boolean val1 = line.length() != 0;
        boolean val2 = !line.startsWith("//");
        System.out.println(val1 + " " + val2);
        br.close();
    }
}

File temp.txt contains first line as- 文件temp.txt包含第一行-

//,<verb>,<verb>

So, the output should be 因此,输出应为

true false

But I get output as 但是我得到的输出为

true true

Can somebody tell me how to fix this? 有人可以告诉我如何解决这个问题吗?

You probably have a BOM (Byte Order Marker) at the beginning of the file. 文件的开头可能有一个BOM(字节顺序标记)

These BOM bytes of UTF-8 are: 0xEF 0xBB 0xBF. UTF-8的这些BOM字节为:0xEF 0xBB 0xBF。 They are just the first 3 bytes in the file added by text editor when saving as UTF-8. 当保存为UTF-8时,它们只是文本编辑器添加的文件的前3个字节。 Possibly your text editor should have option to save UTF-8 text without BOM. 可能您的文本编辑器应该可以选择保存没有BOM的UTF-8文本。

在文本编辑器中打开temp.txt,并确保//前面没有任何字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM