Creating Java UTF-8 Files from an Executable Jar

I have a small Java project where I set the properties of class files in UTF-8 (I use a lot of foreign characters that are not found on CP1252 by default).

The goal is to create a text file (on Windows) containing a list of items. When starting class files from Eclipse itself (pressing Ctrl + F11) it creates the file flawlessly and opens it in another editor (I use Notepad ++). I can see the characters the way I wanted.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          Universidade2010 (18/18)β”‚
β”‚                                         hidden: 0β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

But when I export the project (using Eclipse) as an executable Jar and run it using javaw -jar project.jar, the newly created file is a mess of question marks

????????????????????????????????????????????????????
?                          Universidade2010 (19/19)?
?                                         hidden: 0?
????????????????????????????????????????????????????

, UTF-8 (, -, Java), ,

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

, , Jar

- , Java , UTF-8 ?


, ( )

, ( Charset), :

public class Printer {

    File f;
    FileOutputStream fos;
    Writer w;
    final byte[] utf8_bom = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };

    public Printer(String filename){
        f = new File(filename);
        try {
            fos = new FileOutputStream(f);
            w = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
            fos.write(utf8_bom);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void print(String s) {
        if(fos != null){
            try {
                fos.write(s.getBytes());
                fos.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

:

private final char pipe = '\u2502';         /* β”‚ */
private final char line = '\u2500';         /* ─ */
private final char pipeleft = '\u251c';     /* β”œ */
private final char piperight = '\u2524';    /* ─ */
private final char cupleft = '\u250c';      /* β”Œ */
private final char cupright = '\u2510';     /* ┐ */
private final char cdownleft = '\u2514';    /* β”” */
private final char cdownright = '\u2518';   /* β”˜ */

, , Eclipse, , Jar ( "?" char)

, , Jar , , Eclipse CP1252 - , unicode

+5
1

, UTF-8 (-, - Java)

Java- ( Windows 95). . , Java- .

  private static final String BOM = "\ufeff";

  public static void main(String[] args) throws IOException {
    String data = "\u250c\u2500\u2500\u2510\r\n\u251c\u2500\u2500\u2524";
    OutputStream out = new FileOutputStream("data.txt");
    Closeable resource = out;
    try {
      Writer writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
      resource = writer;
      writer.write(BOM);
      writer.write(data);
    } finally {
      resource.close();
    }
  }

:

β”Œβ”€β”€β”
β”œβ”€β”€β”€

Windows, Notepad, .

.

- , Java , UTF-8 ?

- . file.encoding , .


.


:

public class Printer implements Closeable {
  private PrintWriter pw;
  private boolean error;

  public Printer(String name) {
    try {
      pw = new PrintWriter(name, "UTF-8");
      pw.print('\uFEFF'); // BOM
      error = false;
    } catch (IOException e) {
      error = true;
    }
  }

  public void print(String s) {
    if (pw == null) return;
    pw.print(s);
    pw.flush();
  }

  public boolean checkError() { return error || pw.checkError(); }

  @Override public void close() { if (pw != null) pw.close(); }
}

, , PrintWriter. , ( ).

+6

All Articles