我有一些当前代码,问题是它创建了一个 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();
不要使用
FileWriter
,而是创建一个 FileOutputStream
。然后,您可以将其包装在 OutputStreamWriter
中,这允许您在构造函数中传递编码。然后您可以将数据写入 try-with-resources 语句:
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();
}
FileUtils.write
。
您应该能够执行以下操作:
File f = new File("output.txt");
FileUtils.writeStringToFile(f, document.outerHtml(), "UTF-8");
如果文件不存在,这将创建该文件。
从 Java 7 开始,您可以使用
Files.newBufferedWriter
做同样的事情,更简洁一点:
Path logFile = Paths.get("/tmp/example.txt");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
writer.write("Hello World!");
// ...
}
这里给出的所有答案都不起作用,因为 java 的 UTF-8 编写有问题。
http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html
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();
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));
}
}
Java 8 版本 允许您省略
Charset
参数 - 方法默认为 UTF-8。
Files.write(textFile, lines);
我们可以使用java编写UTF-8编码的文件 使用 PrintWriter 写入 UTF-8 编码的 xml
或点击这里
PrintWriter out1 = new PrintWriter(new File("C:\\abc.xml"), "UTF-8");
下面的示例代码可以逐行读取文件并以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();
}
}
以下是在 Eclipse IDE 中将 UTF-8 字符写入文件的示例。
对于 Eclipse。只需从
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);
}
}
}
}