Encoding cp-1252 as utf-8?

前端 未结 2 1074
既然无缘
既然无缘 2020-12-20 02:22

I am trying to write a Java app that will run on a linux server but that will process files generated on legacy Windows machines using cp-1252 as the character set. Is there

2条回答
  •  暖寄归人
    2020-12-20 03:22

    You can read and write text data in any encoding that you wish. Here's a quick code example:

      public static void main(String[] args) throws Exception
      {
        // List all supported encodings
        for (String cs : Charset.availableCharsets().keySet())
          System.out.println(cs);
    
        File file = new File("SomeWindowsFile.txt");
        StringBuilder builder = new StringBuilder();
    
        // Construct a reader for a specific encoding
        Reader reader = new InputStreamReader(new FileInputStream(file), "windows-1252");
        while (reader.ready())
        {
          builder.append(reader.read());
        }
        reader.close();
    
        String string = builder.toString();
    
        // Construct a writer for a specific encoding
        Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8");
        writer.write(string);
        writer.flush();
        writer.close();
      }
    

    If this still 'chokes' on read, see if you can verify that the the original encoding is what you think it is. In this case I've specified windows-1252, which is the java string for cp-1252.

提交回复
热议问题