java - Read and write file with windows-1252 -


i'm trying write file containing german characters disk , read using windows-1252 encoding. don't understand why, output this:

<title>w�hrend und im anschluss die exkursion stehen ihnen die ansprechpartner f�r o-t�ne</title>  <p>die themen im �berblick</p> 

any thoughts? here code. you'll need spring-core , commons-io run it.

private static void write(string filename, charset charset) throws ioexception {     string html = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" +                   "<head>" +                   "<meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1252\">" +                   "<title>während und im anschluss die exkursion stehen ihnen die ansprechpartner für o-töne</title>" +                   "</head>" +                   "<body>" +                   "<p>die themen im Überblick</p>" +                   "</body>" +                   "</html>";      byte[] bytes = html.getbytes(charset);     fileoutputstream outputstream = new fileoutputstream(filename);     outputstreamwriter writer = new outputstreamwriter(outputstream, charset);     ioutils.write(bytes, writer);     writer.close();     outputstream.close(); }  private static void read(string file, charset windowscharset) throws ioexception {     classpathresource pathresource = new classpathresource(file);     string string = ioutils.tostring(pathresource.getinputstream(), windowscharset);     system.out.println(string); }  public static void main(string[] args) throws ioexception {     charset windowscharset = charset.forname("windows-1252");     string file = "test.txt";     write(file, windowscharset);     read(file, windowscharset); } 

your write method wrong. using writer write bytes. writer should used writing characters or strings.

you encoded string bytes line

byte[] bytes = html.getbytes(charset); 

these bytes can written output stream:

ioutils.write(bytes, outputstream); 

this makes writer unnecessary (remove it) , correct output.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -