0
File file = new File("file.txt");
BufferedWriter output=new BufferedWriter(new Filewriter(file));

output.write("data fetched from database");

I am fetching data from database which is mapped to corresponding DTO object and writing the same to the file using the getters of the DTO object.

It's working fine for the normal data, but fails for the data as below:

the word in DB is : KÄRNTEN.

When I open the file in Windows in notepad, it is proper.

When I open in vi or vim editor, after KÄ, it is going to a new line and writing the remaining.

when I open in wordpad or csv file in Windows, it's displayed as : KÄRNTEN.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112

1 Answers1

0

The effect looks like You are getting the data as UTF-8, but read it as ISO-8859.

Try

Writer out = new BufferedWriter(new OutputStreamWriter(
             new FileOutputStream("file.txt"), "ISO-8859-1"));
try {
   out.write("data fetched from database:" + yourFetchedData);
} finally {
   out.close();
}

See also How to write a UTF-8 file with Java?

Community
  • 1
  • 1
Martin G
  • 229
  • 1
  • 6
  • I had even tried OutStreamWriter but no luck. Later , I found that it's not the problem with the file.I changed terminal settings of putty and now, the data looks clean. On terminal, change settings-->translation--> and selected UTF-8 from the drop down. – avinashsv Mar 06 '17 at 17:23