Java 我将用不同的语言在服务器上编写文件(日志)。英语“u name.txt是可以的,”عاتبوها;。txt";正在显示“什么?”;?????。txt";在服务器上
要以不同的语言登录服务器时,会显示“عاتتوها.txt”,但服务器上会显示“???????.txt” 在服务器上写入日志文件的代码如下:Java 我将用不同的语言在服务器上编写文件(日志)。英语“u name.txt是可以的,”عاتبوها;。txt";正在显示“什么?”;?????。txt";在服务器上,java,tigase,Java,Tigase,要以不同的语言登录服务器时,会显示“عاتتوها.txt”,但服务器上会显示“???????.txt” 在服务器上写入日志文件的代码如下: BufferedWriter bw = null; FileWriter fw = null; String label = "label test"; // this content comming dynamic value from another method. String content = "هذا هو اختبار عينة يرجى
BufferedWriter bw = null;
FileWriter fw = null;
String label = "label test";
// this content comming dynamic value from another method.
String content = "هذا هو اختبار عينة يرجى اختبار";
// this content comming dynamic value from another method.
String room = "اختبار";
Date date = new Date();
try {
String dt = sdf.format(date);
//String convertedRoom = new String(room.getBytes("UTF-8"));
String fileName = room.concat("_").concat(dt).concat(".txt");
File f = new File(fileName);
if(!f.exists()) {
f.createNewFile();
f.setWritable(true);
}
fw = new FileWriter(fileName,true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
} catch (IOException e) {
//e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
//ex.printStackTrace();
}
}
现在,服务器上的日志生成正常,文件中的日志文件上的数据也正常。。。但问题是文件名不是用另一种语言生成的,比如arebic.txt,它显示的是什么。。。请提供帮助。首先有一个小问题,java源代码必须按照java编译器编译的相同编码(字符集)进行编辑,以便正确翻译java源代码中的阿拉伯语文本。 这可以通过比较阿拉伯文中的字符串文字与带u-转义阿拉伯文的字符串文字来测试:
“\u…\u…”
这里有两个问题:文件名和文件内容。
对于文件名,请检查上述小问题。如果这没有帮助,您可能需要在UTF-8区域设置中运行应用程序服务器;查看Linux帮助
在Windows下,文件名在我的位置确实不起作用。您可以编写一个带有拉丁字符的zip文件,其中包含UTF-8文件名
必须使用转换写入文件内容,以便生成的字节为UTF-8(或阿拉伯语Windows-1256)。这里FileWriter/FileReader不可用,因为它们使用默认编码,并且不能像其他类那样指定编码
String room = "اختبار";
System.out.println("room = " + uescape(room));
room = "\u0627\u062e\u062a\u0628\u0627\u0631";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String dt = sdf.format(date);
String fileName = room + "_" + dt + ".txt";
Path f = Paths.get(fileName);
try (BufferedWriter bw = Files.newBufferedWriter(f, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
bw.write("\r\n=s=" + label + "====" + date.toString() + "====>");
bw.write(content);
bw.write("=e=" + date.toString() + "===>\r\n");
} catch (IOException e) {
// FIXME Do something with the exception...
}
}
private static String uescape(String room) {
StringBuilder sb = new StringBuilder();
sb.append('\"');
for (char ch : room.toCharArray()) {
if (32 <= ch && ch < 127) {
sb.append(ch);
} else {
sb.append(String.format("\\u%04x", 0xFFFF & ch));
}
}
sb.append('\"');
return sb.toString();
}
弦乐室;
System.out.println(“room=“+uescape(room));
room=“\u0627\u062e\u062a\u0628\u0627\u0631”;
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
日期=新日期();
字符串dt=sdf.format(日期);
字符串文件名=房间+“”+dt+”.txt”;
路径f=路径.get(文件名);
try(BufferedWriter bw=Files.newBufferedWriter(f,StandardCharsets.UTF_8,
StandardOpenOption.CREATE,StandardOpenOption.APPEND){
写入(“\r\n=s=“+label+”==“+date.toString()+”=>”;
写(内容);
write(“=e=“+date.toString()+”==>\r\n”);
}捕获(IOE异常){
//修正我做一些例外。。。
}
}
私人静态字符串uescape(字符串室){
StringBuilder sb=新的StringBuilder();
某人附加(“\”);
for(char ch:room.toCharArray()){
如果(32)您认为字符串convertedRoom=新字符串(room.getBytes(“UTF-8”))是吗?删除它,它是不必要的,并且可能有害。尝试将utf-8用于arebic语言。这完全是错误的。去掉它。你是如何想出这样的想法的,它不起作用的。好吧,试试你的解决方案,然后这样做。这不是一个解决方案。这是一个关于你的代码完全错误的评论,你似乎没有放弃了解您所做的一切。找不到符号[ERROR]符号:类路径找不到符号[ERROR]符号:变量标准字符集找不到符号[ERROR]符号:变量标准OpenOption:请帮助需要导入类您的IDE有一些“修复导入”"操作;
import java.nio.file.Path;
等等。添加了import java.nio.charset.standardcharset;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.StandardOpenOption;import java.text.SimpleDateFormat;已测试此代码并在非cr的服务器上运行正在读取服务器上的文件:(