[英]How to save text file with UTF-8 encoding in java?
I am facing a problem in saving a text file in UTF-8 format using java. 我在使用Java以UTF-8格式保存文本文件时遇到问题。 When i click on save as for the generated text file, it gives ANSI as text format but not as UTF-8. 当我单击另存为生成的文本文件时,它以文本格式提供ANSI,但不提供UTF-8格式。 Below is the code i am writing while creating the file: 以下是我在创建文件时编写的代码:
String excelFile="";
excelFile = getFullFilePath(fileName);
File file = new File(excelFile + fileName,"UTF-8");
File output = new File(excelFile,"UTF-8");
FileUtils.writeStringToFile(file, content, "UTF-8");
While creating the text file, I am using UTF-8 encoding, but the file still shows the encoding as ANSI while saving. 创建文本文件时,我使用的是UTF-8编码,但是保存时文件仍将编码显示为ANSI。 Kindly help. 请帮助。
Instead of using File, create a FileOutputStream. 代替使用File,而是创建FileOutputStream。 Try this. 尝试这个。
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("outfilename"), "UTF-8"));
try {
out.write(aString);
} finally {
out.close();
}
I had a problem very similar to that and I solved saving the original file with the UTF-8 encoding. 我遇到的问题与此非常相似,因此解决了使用UTF-8编码保存原始文件的问题。 In this case, go to the excel file and save it again, or create a new file and copy the content, and make sure that its encoding is UTF-8. 在这种情况下,请转到excel文件并再次保存,或者创建一个新文件并复制内容,并确保其编码为UTF-8。 This link has a tutorial on how to save an excel file with UTF-8 encoding: https://help.surveygizmo.com/help/encode-an-excel-file-to-utf-8-or-utf-16 . 此链接包含有关如何使用UTF-8编码保存Excel文件的教程: https : //help.surveygizmo.com/help/encode-an-excel-file-to-utf-8-or-utf-16 。 At the most your code seems to be correct. 最多,您的代码似乎是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.