This discussion is archived
3 Replies Latest reply: Feb 2, 2007 12:51 AM by 807607 RSS

Writing to file with specific encoding in unix

807607 Newbie
Currently Being Moderated
hi,
I want to write html files which contain Turkish characters in unix operating system.
I'm currently using FileWriter to write the files.
I'm getting the file content from the database and I can see that the characters seem to be fine but when I write them into an html file, they are displayed with the question mark character (?).
What can I do about this?

Thanks
  • 1. Re: Writing to file with specific encoding in unix
    807607 Newbie
    Currently Being Moderated
    here is how I write the file

    File f = new File(Sabit.GECICI_DOWNLOAD_PATH,System.currentTimeMillis()+".html");
    FileWriter fw = new FileWriter(f);
    fw.write(html.toString());
    fw.close();
  • 2. Re: Writing to file with specific encoding in unix
    807607 Newbie
    Currently Being Moderated
    Write the file using an encoding that supports Turkish. e.g.
    Writer writer = new OutputStreamWriter(new FileOutputStream("the file name"), "utf-8"); // utf-8 is the encoding and will support any UNICODE
    P.S. Anything you view the file with must support "utf-8" encoding and a font that supports Turkish characters.
  • 3. Re: Writing to file with specific encoding in unix
    807607 Newbie
    Currently Being Moderated
    FileWriter uses the default character encoding of whatever platform your program is running on. Beside the fact that the default encoding may vary from one machine to another, it may not be suitable for storing non-ASCII (Turkish) characters.

    Rather than using FileWriter, you can use an OutputStreamWriter and a FileOutputStream, which allows you to specify the character encoding you want. UTF-8 is generally a good choice, since it can encode just about anything. For example:
    FileOutputStream fos = new FileOutputStream("/some/file/name");
    OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
    osw.write("your text with Turkish characters...");
    Geoff