我希望代码包含任何java.io包或其他包(如果可用), 阅读和写一个字符串,如“سلامبههمهدوستانم”(波斯语) 从文件。但我没有找到一个好方法。 问题是保存到文件:它显示?之后在像记事本这样的程序中 谢谢你们 这是我试过的代码:
public static int saveInputToFile(String filelocation) throws IOException
{
// it will save from console to file;
// temp data
// int buffersize = 1;
// char[] buffer = new char[buffersize];
//
// create open file, save userInput(an string) into the file
// create file:
File file = new File(filelocation);// to position: file exist, then
// opens, file not exist then
// created and opens
// read one line data
InputStreamReader reader = new InputStreamReader(System.in, "UTF-8");
BufferedReader bufReader = new BufferedReader(reader);
String userInputLine = bufReader.readLine();
//
// write one line data to the file
OutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(userInputLine);
writer.flush();
writer.close();
// goal achieved data has been saved to file.
return 0;
}
public static int saveInputToFileVer2(String filelocation)
throws FileNotFoundException, UnsupportedEncodingException
{
// OutputStream outSTream = new FileOutputStream(filelocation);
PrintWriter writer = new PrintWriter(filelocation, "UTF-8");
//
String data = "";
Scanner scan = new Scanner(System.in, "UTF-8");
LOOP: while (true) {
System.out.println("please enter your data to save:");
data += scan.nextLine();
System.out.println("do you want to append other data? yes:y no:n");
char choice = scan.nextLine().charAt(0);
if (choice == 'y') {
continue;
} else {// choise = 'n'
break LOOP;
}
}
writer.println(data);
writer.flush();
writer.close();
System.out.println("write ended successfully");
// end
return 0;
}
答案 0 :(得分:1)
删除" UTF-8"从流中并将其放入将要写入文件的字符串中 注意用阿拉伯语或波斯语字母书写英语有时会引起一些冲突
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(reader);
String userInputLine = bufReader.readLine();
//
// write one line data to the file
OutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write(new String(userInputLine.getBytes("UTF-8")));
writer.flush();
答案 1 :(得分:0)
1 - 诀窍是以阿拉伯语存储并以波斯语显示
将文字从阿拉伯语改为波斯语
text.trim().replaceAll("ی", "ي").replaceAll("ک", "ك");
将文字从波斯语改为阿拉伯语
text.trim().replaceAll("ي", "ی").replaceAll("ك","ک");
2 - 另一种方式是以base64格式存储并存储它