Java – the previously created encoding file cannot be read correctly

I have a problem reading the encoded file. I have written my own code before

Display the original string correctly (including Accents)

The code I save string to the encoding file is as follows:

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileName),"ISO-8859-1");
writer.write(text);

Then, I read such a file:

InputStream is = getClass.getResourceAsStream(fileName);

try {           
    BufferedReader br = new BufferedReader(new InputStreamReader(is,"ISO-8859-1"));
    String line;
    StringBuilder sb = new StringBuilder();

    while( (line = br.readLine()) != null ) {
        sb.append(line);
    }

    String result = sb.toString();
} catch (UnsupportedEncodingException e3) {
} catch (IOException e) { }

The string result is not displayed correctly For example, accents are missing

I also tried other methods, such as encoding string into bytes, and then writing these bytes to the file I always get the same result as other ISO codes Any ideas?

Solution

The problem is that your string has different character sets, which may be utf-16 Output text to the desired character set

This answer proves syntax

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>