-1

I am trying to take a string encoded and saved in Cp1252 encoding and display it in a java text area. When i read it back it has black diamonds with question marks where special characters normally are (', &, etc). What should i do to format it to display the proper characters.

I cant copy and paste the text as it displays almost properly when moved out of word. But the code i am using to read the Cp1252 file is below:

try {
        br = new BufferedReader(new FileReader(f));
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            emi.stringContent += "\n" + strLine;
        }

    br.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex);
    }

Thanks!

Made the following edits and now nothing reads in.

StringBuffer temp2 = new StringBuffer(1000);
    int numRead = 0;
    char[] buf = new char[1024];

    try {
        ir = new InputStreamReader(new FileInputStream(f), "Cp1252");
        while((numRead = ir.read()) != -1)
        {
            String temp = String.valueOf(buf, 0, numRead);
            temp2.append(temp);
            buf = new char[1024];
        }
        emi.stringContent = temp2.toString();

Lines appear to be skipped StringBuffer temp2 = new StringBuffer();

    try {
        ir = new InputStreamReader(new FileInputStream(f), "Cp1252");
        br = new BufferedReader(ir);
        while(br.readLine() != null)
        {
            temp2.append(br.readLine());
        }
        emi.stringContent = temp2.toString();
  • possible duplicate of [Java FileReader encoding issue](http://stackoverflow.com/questions/696626/java-filereader-encoding-issue) – Brian Roach May 10 '13 at 03:18

1 Answers1

0

Rather than using a FileReader, use a FileInputStream wrapped with an InputStreamReader; specify the character encoding in the InputStreamReader's constructor (according to this page it looks like you should use "Cp1252" for the encoding)

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • Try wrapping the `InputStreamReader` in a [BufferedReader](http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html), and use the BufferedReader's `readLine()` method in the loop (terminating the loop when `readLine()` reads a null). – Zim-Zam O'Pootertoot May 10 '13 at 03:48
  • that works but i lose all formatting...i need to keep the formatting – parkjohnston May 10 '13 at 03:52
  • You're skipping every other line of text - br.readLine() consumes a line of text, so the br.readLine() statement in the loop header is discarding lines of text. Change this to `String tempStr; while((tempStr = br.readLine()) != null) { temp2.append(tempStr); }` – Zim-Zam O'Pootertoot May 10 '13 at 04:03