如何用Java编写UTF-8文件?

时间:2009-06-16 13:39:06

标签: java file-io utf-8

我有一些当前的代码,问题是它创建了一个1252代码页文件,我想强制它创建一个UTF-8文件

任何人都可以帮我这个代码,因为我说它目前有效...但我需要强制保存在utf ..我能传递一个参数或什么?

这就是我所拥有的,任何帮助都非常感激

var out = new java.io.FileWriter( new java.io.File( path )),
        text = new java.lang.String( src || "" );
    out.write( text, 0, text.length() );
    out.flush();
    out.close();

9 个答案:

答案 0 :(得分:192)

试试这个

Writer out = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream("outfilename"), "UTF-8"));
try {
    out.write(aString);
} finally {
    out.close();
}

答案 1 :(得分:189)

不是使用FileWriter,而是创建FileOutputStream。然后,您可以将其包装在OutputStreamWriter中,这允许您在构造函数中传递编码。然后,您可以将数据写入try-with-resources Statement

内的数据
try (OutputStreamWriter writer =
             new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8))
    // do stuff
}

答案 2 :(得分:23)

尝试使用Apache Commons中的FileUtils.write

您应该可以执行以下操作:

File f = new File("output.txt"); 
FileUtils.writeStringToFile(f, document.outerHtml(), "UTF-8");

如果文件不存在,这将创建文件。

答案 3 :(得分:21)

这里给出的所有答案都不会起作用,因为java的UTF-8写作被窃听了。

http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html

答案 4 :(得分:17)

从Java 7开始,你可以更简洁地对Files.newBufferedWriter做同样的事情:

Path logFile = Paths.get("/tmp/example.txt");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
    writer.write("Hello World!");
    // ...
}

答案 5 :(得分:9)

var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();

答案 6 :(得分:7)

Java 7 Files utility type对于处理文件非常有用:

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.*;

public class WriteReadUtf8 {
  public static void main(String[] args) throws IOException {
    List<String> lines = Arrays.asList("These", "are", "lines");

    Path textFile = Paths.get("foo.txt");
    Files.write(textFile, lines, StandardCharsets.UTF_8);

    List<String> read = Files.readAllLines(textFile, StandardCharsets.UTF_8);

    System.out.println(lines.equals(read));
  }
}

Java 8 version允许您省略 Charset 参数 - 默认为UTF-8的方法。

答案 7 :(得分:3)

我们可以使用java编写UTF-8编码文件 使用PrintWriter编写UTF-8编码的xml

或点击here

PrintWriter out1 = new PrintWriter(new File("C:\\abc.xml"), "UTF-8");

答案 8 :(得分:2)

下面的示例代码可以逐行读取文件并以UTF-8格式写入新文件。另外,我明确指定了Cp1252编码。

    public static void main(String args[]) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(
            new FileInputStream("c:\\filenonUTF.txt"),
            "Cp1252"));
    String line;

    Writer out = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(
                    "c:\\fileUTF.txt"), "UTF-8"));

    try {

        while ((line = br.readLine()) != null) {

            out.write(line);
            out.write("\n");

        }

    } finally {

        br.close();
        out.close();

    }
}