views:

175

answers:

2

I am trying to write greek characters to a file using java like this:

String greek = "\u03c1\u03ae\u03bc. \u03c7\u03b1\u03b9\u03c1\u03b5\u03c4\u03ce";

 try {
         BufferedWriter out = new BufferedWriter(new FileWriter("E:\\properties\\outfilename.txt"));
         out.write(greek);
         out.close();
     } catch (IOException e) {
     }

Not working. Tried to use javac -encoding ISO-8859-7. Also tried java -Dfile.encoding=ISO-8859-7. Assuming that as I do not have greek font in my pc, I downloaded achillies (greek font - Ach4.ttf).Installed it by going to control panel> fonts. Any ideas?

+1  A: 

Try something like:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\properties\\outfilename.txt"), "ISO-8859-7"));
danben
A: 

An empty catch block is very bad practice. Likely you're having an IOException but aren't being made aware of it because you told Java to throw it away.

Your code works fine for me, so I'm guessing the problem is with the only thing I changed: The file path.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Sandbox {
    public static void main(String[] args) {
        try {
            final String greek = "\u03c1\u03ae\u03bc. " +
                    "\u03c7\u03b1\u03b9\u03c1\u03b5\u03c4\u03ce";
            final BufferedWriter out 
                = new BufferedWriter(new FileWriter("outfilename.txt"));

            try { out.write(greek); }
            finally { out.close(); }
        }
        catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}
Gunslinger47
+1 - it is so ironic when coding abominations like "try/catch/ignore" rebound on the programmers who created them ...
Stephen C
I'm not sure what the reason is for the two downmods. Do some of you doubt that an IOException is at fault, or is the a problem with my sample code?
Gunslinger47