views:

56

answers:

3

In Java, I am trying to parse an HTML file that contains complex text such as greek symbols.

I encounter a known problem when text contains a left facing quotation mark. Text such as

mutations to particular “hotspot” regions

becomes

 mutations to particular “hotspot�? regions

I have isolated the problem by writting a simple text copy meathod:

public static int CopyFile()
{
    try
    {
    StringBuffer sb = null;
    String NullSpace = System.getProperty("line.separator");
    Writer output = new BufferedWriter(new FileWriter(outputFile));
    String line;
    BufferedReader input =  new BufferedReader(new FileReader(myFile));
while((line = input.readLine())!=null)
    {
        sb = new StringBuffer();
        //Parsing would happen
        sb.append(line);
        output.write(sb.toString()+NullSpace);
    }
        return 0;
    }
    catch (Exception e)
    {
        return 1;
    }
}

Can anybody offer some advice as how to correct this problem?

+5  A: 

The file read is not in the same encoding (probably UTF-8) as the file written (probably ISO-8859-1).

Try the following to generate a file with UTF-8 encoding:

BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile),"UTF8"));

Unfortunately, determining the encoding of a file is very difficult. See http://stackoverflow.com/questions/499010/java-how-to-determine-the-correct-charset-encoding-of-a-stream

Thierry-Dimitri Roy
And as far as I know there isn't really an automated way to obtain the encoding of a text file.
extraneon
`Reader` end of things too.
Tom Hawtin - tackline
"UTF8" and 16 don't seem to work even though <meta http-equiv="content-type" content="text/html;charset=utf-8" /> is explicitely stated in the HTML...Does anybdoy know how to look up encoding by going from a know character in a file to an encoding?
Misha
I tried US-ASCII ISO-8859-1UTF-8UTF-16BEUTF-16LE UTF-16And they don't work...
Misha
The character's decimal value is "8221", it should be Unicode right?
Misha
Problem was solved by changing to UTF-8 AND parsing the entire file and replacing all special above 126 characters to "xx" format.
Misha
A: 

In addition to what Thierry-Dimitri Roy wrote, if you know the encoding you have to create your FileReader with a bit of extra work. From the docs:

Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

extraneon
A: 

The Javadoc for FileReader says:

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

In your case the default character encoding is probably not appropriate. Find what encoding the input file uses, and specify it. For example:

FileInputStream fis = new FileInputStream(myFile);
InputStreamReader isr = new InputStreamReader(fis, "charset name goes here");
BufferedReader input = new BufferedReader(isr);
Richard Fearn