Y.E.P
Y.E.P

Reputation: 1207

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 i.e 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 !

Upvotes: 0

Views: 496

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

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.

Upvotes: 1

Related Questions