简体   繁体   中英

How to read special characters in java

I have this text on a text file which is encoded in UTF-8 o|▰╹ェ╹|ツ end (◕□◕✿) end ┐(❛△❛;) when i try to read this in java the output is always like this ?o|??ェ?|ツ end (?□??) end ┐(?△?;) i even try to encode this in Unicode but still it get the same text here is my code to get print the file

File fileDir = new File("src/mycharacters.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(
        new FileInputStream(fileDir), "UTF-8"));
String myText = null;
String line = null;
while ((line = reader.readLine()) != null) {
    myText = line;
}

System.out.print(myText);  

Your reading is fine. Your writing is the problem. AFAIK System.out has non-UTF encoding on it. Try to wrap it in PrintStream or PrintWriter :

niceOut = new PrintStream(System.out, true, "UTF-8");

EDIT: Oh wait, maybe @MathSquared11235 is actually correct - I do see ツ in your output, which should indicate UTF output. So... Font.

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