Create a bat from the java code keeping quotes and accented characters

advertisements

I have created a java gui to generate bat files. When I write the bat containing a string like this: "L’uomo più forte" notepad++ shows this: "L?uomo pi— forte"

Here is the code:

FileOutputStream fos = new FileOutputStream(bat);
Writer w = new BufferedWriter(new OutputStreamWriter(fos, "Cp850"));
String stringa = "L’uomo più forte"
w.write(stringa);
w.write("\n");
w.write("pause");
w.write("\n");
w.flush();
w.close();

I had to use cp850 for dos use. Using base charset the bat give error.

Solutions?


Instead of using "Cp850":

Writer w = new BufferedWriter(new OutputStreamWriter(fos, "Cp850"));

Try using "UTF-8":

Writer w = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));

For more information on UTF-8 Encoding Go Here.

Don't forget to place a semicolon (;) at the end of your stringa string variable declaration/initialization and surround your code in try/catch block to handle possible FileNotFoundException, UnsupportedEncodingException, and IOException Exceptions.

Also...for NotePad, to provide a new line you need to also supply the \r tag:

w.write("\r\n");