简体   繁体   中英

java utf-8 file reading

I am trying to read a UTF-8 encoded file as follows-

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-

//,<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.

These BOM bytes of UTF-8 are: 0xEF 0xBB 0xBF. They are just the first 3 bytes in the file added by text editor when saving as UTF-8. Possibly your text editor should have option to save UTF-8 text without BOM.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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