[英]How to write a UTF-8 file with Java?
我有一些當前代碼,問題是它創建了一個 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();
創建一個FileOutputStream
而不是使用FileWriter
。 然后,您可以將其包裝在一個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();
}
嘗試使用 Apache Commons 中的FileUtils.write
。
您應該能夠執行以下操作:
File f = new File("output.txt");
FileUtils.writeStringToFile(f, document.outerHtml(), "UTF-8");
如果文件不存在,這將創建該文件。
從 Java 7 開始,你可以更簡潔地對Files.newBufferedWriter
做同樣的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 Files 實用程序類型對於處理文件很有用:
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。
我們可以使用 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();
}
}
下面是將 UTF-8 個字符寫入 Eclipse IDE 和一個文件的例子。
對於 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);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.