简体   繁体   English

将Windows-1252文件转换为UTF-8文件

[英]Convert Windows-1252 file into UTF-8 file

Hello I am having some issues with this simple task of conversion. 你好,我在这个简单的转换任务中遇到了一些问题。 Here is my code bellow (rough but not so complex): 这是我的代码(粗略但不那么复杂):

        FileInputStream fis = new FileInputStream ("file");
    BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"CP1250"));

    try {

        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while (line != null) {
            sb.append(line);
            if(line.contains(" "))
            sb.append(System.lineSeparator());
            try {
                line = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        String everything = sb.toString();
        System.out.println(everything);

        PrintWriter writer = null;
        try {
            writer = new PrintWriter("clean", "UTF-8");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        writer.println(everything);
        writer.close();
    } 

    finally {
        try {
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

But I get the same output as the input with the same encoding format. 但我获得与输入相同的输出具有相同的编码格式。 Do you see anyway able to help? 你觉得无论如何能帮忙吗?

The docs say that 1) public void println(String x) Prints a String and then terminates the line. 文档说1)public void println(String x)打印一个String然后终止该行。 This method behaves as though it invokes print(String) and then println(). 此方法的行为就像调用print(String)然后调用println()一样。

And 2) public void print(String s) Prints a string. 2)public void print(String s)打印一个字符串。 If the argument is null then the string "null" is printed. 如果参数为null,则打印字符串“null”。 Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method. 否则,根据平台的默认字符编码将字符串的字符转换为字节,并且这些字节的写入方式与write(int)方法完全相同。

You probably will get your conversion done with 您可能会完成转换

PrintWriter writer 
    = new PrintWriter(new OutputStreamWriter(new FileOutputStream("clean", true), 
        "UTF-8")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM