Question

Following does not work on linux machine.

        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();

        try {
            FileOutputStream fo = new FileOutputStream("hi.txt");
            PrintStream ps = new PrintStream(fo);
            String msgBody = "ΣYMMETOXH";
            ps.println(decoder.decode(ByteBuffer.wrap(decoder.decode(ByteBuffer.wrap(msgBody.getBytes())).toString().getBytes())));
            ps.close();
            fo.close();
        } catch (CharacterCodingException e) {
            e.printStackTrace();
        }

This code works on windows. What can be the issue? On linux machine decoder does not decode the string.

Was it helpful?

Solution

The problem is that you're using String.getBytes() at least once, possibly twice (your enormously long line is hard to read; using several statements would make it easier to understand). That doesn't specify an encoding, so it'll use the platform default encoding. At that point, you've got a platform dependency... hence the problem.

It's not at all clear what you're trying to achieve, but if you're looking for reasons for platform-specific behaviour, that's the first thing to look at.

Oh, and creating a PrintStream like that will have the same issue... create an OutputStreamWriter with a specific encoding instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top