如何用 Java 编写 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();
409776 次浏览

不要使用 FileWriter,而是创建一个 FileOutputStream。然后您可以将其包装在 OutputStreamWriter中,这允许您在构造函数中传递编码。然后你可以把你的数据写到一个 试用资源声明里面:

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

试试这个

Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("outfilename"), "UTF-8"));
try {
out.write(aString);
} finally {
out.close();
}
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();

尝试使用 Apache Commons 中的 FileUtils.write

你应该可以这样做:

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

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

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

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

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

或按 给你

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

Java7文件实用工具类型对于处理文件非常有用:

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

Java8版本允许您省略 Charset参数-UTF-8的默认方法。

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

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

下面的示例代码可以逐行读取文件并以 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();


}
}

下面是在 EclipseIDE 中编写 UTF-8字符并将其写入 File 的示例。

对于 Eclipse. 只需将 Encoding 从 < strong > Run -> Run Configurations -> Common设置为 UTF-8 公共对话

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class UTF_8_Example {


/**
* Example of printing UTF-8 characters inside Eclipse IDE and a File.
* <p>
* For eclipse, you must go to Run ->  Run Configurations -> Common
* and set Encoding to UTF-8.
* <p>
* @param args
*/
public static void main(String[] args) {
BufferedWriter writer = null;


try {
///////////////////////////////////////////////////////////////////
// WRITE UTF-8 WITHIN ECLIPSE EDITOR
///////////////////////////////////////////////////////////////////
char character = '►';
int code = character;
char hex = '\u25ba';
String value = "[" + Integer.toHexString(code) + "][\u25ba][" + character + "][" + (char)code + "][" + hex + "]";
System.out.println(value);


///////////////////////////////////////////////////////////////////
// WRITE UTF-8 TO A FILE
///////////////////////////////////////////////////////////////////
File file = new File("UTF_8_EXAMPLE.TXT");
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
writer = new BufferedWriter(outputStreamWriter);
writer.write(value);
}
catch(Throwable e) {
throw new RuntimeException(e);
}
finally {
try {
if(writer != null) { writer.close(); }
}
catch(Throwable e) {
throw new RuntimeException(e);
}
}
}
}