This discussion is archived
3 Replies Latest reply: Nov 15, 2011 5:19 AM by StanislavL RSS

Wrong characters while trying to display German characters in TextArea

831242 Newbie
Currently Being Moderated
Hi Experts,

I'am trying to read a text file and display this in a TextArea

However the german characters in the text file are not populated properly.

I have the below in the text file.
Flüchtlingsabkommen
When I try to read this and display the output, I get the below
Fl"chtlingsabkommen
The above is the same whether I display the output with System.out.println
or setText in TextArea.

Can you please help.

Thanks

KR
  • 1. Re: Wrong characters while trying to display German characters in TextArea
    StanislavL Pro
    Currently Being Moderated
    It depends on encoding you use. COuld you show some code how you read the text and set it to the JTextArea.
  • 2. Re: Wrong characters while trying to display German characters in TextArea
    831242 Newbie
    Currently Being Moderated
    Thanks for your reply. My coding is as follows. I have tried all 3 charsets i,e iso-8859-1, windows-1252 and UTF-8
    import java.io.*;
    import java.nio.charset.*;
    
    class FileRead 
    {
       public static void main(String args[])
      {
    
    String totline = " ";
    String fname = "C:\\Documents and Settings\\" + System.getenv("USERNAME") + "\\dvt.txt";
    
          try{
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream(fname);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = 
    new BufferedReader(new InputStreamReader(in , Charset.forName("windows-1252")));
    //iso-8859-1, windows-1252, UTF-8
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
          // Print the content on the console
          System.out.println (strLine);
    totline += strLine;
        }
        //Close the input stream
        in.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
     System.out.println ("now TOTAL STRING IS");
    
     System.out.println (totline);
    
    System.out.println ("End of TOTAL STRING");
      }
    }
    KR
  • 3. Re: Wrong characters while trying to display German characters in TextArea
    StanislavL Pro
    Currently Being Moderated
    May be one of these link can help:
    http://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream
    http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
    http://docs.oracle.com/javase/tutorial/essential/io/file.html

    Or just replace all the chars with umlauts with "\u..."

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points