简体   繁体   中英

compilation error while trying to write UTF-8 text to a text file

In my java source code I wanted to write Non-AscII characters ie some characters in UTF-8 format to a text file using java :

  File f = new File("U.txt");
  FileWriter writer = new FileWriter(f);
  writer.write("लता"); // hindi text
  writer.close();

I am using notepad++ as a text editor and I save the file with encoding UTF-8 . But when I compile the java file I get an error like :

 illegal character: \187
 import java.io.*;
  ^

Where am I making a mistake ? What do i need to do ? This is a compilation error !

My guess is that your editor is putting a BOM at the beginning of your file (which is unnecessary for UTF-8). I believe notepad++ has a "utf-8 without BOM" encoding , which you should use instead.

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