如何用Java编写UTF-8文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1001540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a UTF-8 file with Java?
提问by mark smith
I have some current code and the problem is its creating a 1252 codepage file, i want to force it to create a UTF-8 file
我有一些当前的代码,问题是它创建了一个 1252 代码页文件,我想强制它创建一个 UTF-8 文件
Can anyone help me with this code, as i say it currently works... but i need to force the save on utf.. can i pass a parameter or something??
任何人都可以帮我处理这段代码,正如我所说的,它目前有效……但我需要强制保存 utf ……我可以传递参数或其他东西吗?
this is what i have, any help really appreciated
这就是我所拥有的,任何帮助都非常感谢
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();
采纳答案by skaffman
Instead of using FileWriter
, create a FileOutputStream
. You can then wrap this in an OutputStreamWriter
, which allows you to pass an encoding in the constructor. Then you can write your data to that inside a try-with-resources Statement:
而不是使用FileWriter
,创建一个FileOutputStream
. 然后,您可以将其包装在 中OutputStreamWriter
,这允许您在构造函数中传递编码。然后,您可以将数据写入try-with-resources 语句中:
try (OutputStreamWriter writer =
new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8))
// do stuff
}
回答by Markus Lausberg
Try this
尝试这个
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("outfilename"), "UTF-8"));
try {
out.write(aString);
} finally {
out.close();
}
回答by boxofrats
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();
回答by A_M
Try using FileUtils.write
from Apache Commons.
尝试FileUtils.write
从 Apache Commons 使用。
You should be able to do something like:
您应该能够执行以下操作:
File f = new File("output.txt");
FileUtils.writeStringToFile(f, document.outerHtml(), "UTF-8");
This will create the file if it does not exist.
如果文件不存在,这将创建该文件。
回答by Emperorlou
All of the answers given here wont work since java's UTF-8 writing is bugged.
由于 java 的 UTF-8 编写被窃听,这里给出的所有答案都不起作用。
http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html
http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html
回答by Dharmesh Patel
回答by McDowell
The Java 7 Files utility typeis useful for working with files:
在Java 7的文件实用型处理文件非常有用:
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));
}
}
The Java 8 versionallows you to omit the Charsetargument - the methods default to UTF-8.
在Java的版本8,您可以省略字符集参数-方法的默认为UTF-8。
回答by Nigel_V_Thomas
Since Java 7 you can do the same with Files.newBufferedWriter
a little more succinctly:
从 Java 7 开始,你可以Files.newBufferedWriter
更简洁地做同样的事情:
Path logFile = Paths.get("/tmp/example.txt");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
writer.write("Hello World!");
// ...
}
回答by Ammad
Below sample code can read file line by line and write new file in UTF-8 format. Also, i am explicitly specifying Cp1252 encoding.
下面的示例代码可以逐行读取文件并以 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();
}
}