0

您好,我在这个简单的转换任务中遇到了一些问题。下面是我的代码(粗略但不那么复杂):

        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();
        }
    }

但是我得到与输入相同的输出,具有相同的编码格式。你看到无论如何都能提供帮助吗?

4

1 回答 1

0

文档说 1) public void println(String x) 打印一个字符串,然后终止该行。这个方法的行为就像它调用 print(String) 然后 println()。

并且 2) public void print(String s) 打印一个字符串。如果参数为空,则打印字符串“null”。否则,字符串的字符会根据平台默认的字符编码转换为字节,并且这些字节完全按照 write(int) 方法的方式写入。

您可能会完成转换

PrintWriter writer 
    = new PrintWriter(new OutputStreamWriter(new FileOutputStream("clean", true), 
        "UTF-8")); 
于 2015-02-17T13:10:23.720 回答