Results 1 to 4 of 4
  1. #1
    jody is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default reading and writing umlauts

    Hi

    I need to be able to read text which may contain german umlauts, reformat it and write it to an output file.
    I have no possibility to set the locale of the machine.
    Whatever i try, my output text contains '?' instead of umlauts.
    I check this in xemacs - there i see the umlauts correctly in the input file, but only the '?' in the output file.

    Here's the code of a minimal program reproducing this issue:
    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.Locale;
    
    public class Umlaut {
    
    
        static public void inOut(String sIn, String sOut) {
            try {
                PrintWriter pw  =  new PrintWriter(sOut);
                FileReader fr = new FileReader(sIn);
                BufferedReader br = new BufferedReader(fr);
                String sLine = br.readLine();
                while (sLine != null) {
                    sLine="[" + sLine +"]";
                    pw.println(sLine);
                    sLine = br.readLine();
                }
                pw.close();
                br.close();
            } catch (IOException e) {
                System.out.println("IOExceprion:"+e.getMessage());
            }
        }
            
        
        public static void main(String asArgs[]) {
            if (asArgs.length > 1) {
                Locale locCH = new Locale("de", "CH");
                Locale.setDefault(locCH); 
    
                inOut(asArgs[0], asArgs[1]);
            } else {
                System.out.println("Provide input file and output file\n");
            }
        }
    }
    Can anybody tell me what i must do?

    Thank You
    Jody

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: reading and writing umlauts

    What says System.out.println(fr.getEncoding()); ?
    What happens if you specify the encoding/charset explicitly?
    PrintWriter pw = new PrintWriter(sOut,"UTF-8");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sIn), "UTF-8"));

  3. #3
    jody is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: reading and writing umlauts

    Quote Originally Posted by eRaaaa View Post
    What says System.out.println(fr.getEncoding()); ?
    It says
    ASCII
    What happens if you specify the encoding/charset explicitly?
    With the explicit charset all Umlauts in my input file
    Java Code:
    das ist a-umlaut: 
    das ist o-umlaut: 
    das ist u-umlaut: 
    are changed to the same triplet of characters (an i with umlauts, followed by an upside down question mark ,followed by 1/2):
    Java Code:
    [das ist a-umlaut: �]
    [das ist o-umlaut: �]
    [das ist u-umlaut: �]
    When i use explicit UTF-16 on PrintWriter and FileInputStream the umlauts are translated correcrtly, but the three lines are interpreted as 1 line,
    and there are extra characters at the beginning and the end:
    Java Code:
    ^@[das ist a-umlaut: 
    das ist o-umlaut: 
    das ist u-umlaut: 
    ^@]^@
    (The ^@ seems to be a ctrl sequence - i couldn't copy paste it in here, but had to type it)

    When i mix UTF-8 and UTF-16 i get completely garbled output, or text where every second character is "^@".

    Is there perhaps another possibility?

    Thank You
    Jody

  4. #4
    jody is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: reading and writing umlauts

    Hi

    I played around a little more with different encodings and found the solution:
    if i use "ISO-8859-1" for both PrintWriter and InputStream, it works!

    Thanks for the info about the encodings!

    Jody

Similar Threads

  1. Need help reading and writing with textfiles
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 02-08-2012, 07:25 AM
  2. Writing and reading to/from a 2D array!
    By celebor in forum New To Java
    Replies: 11
    Last Post: 06-17-2010, 01:48 PM
  3. Reading and Writing to XML - Help Please!
    By JonnySnip3r in forum New To Java
    Replies: 4
    Last Post: 01-17-2010, 10:55 PM
  4. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •