作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想要一个包含任何类 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;
}
最佳答案
从流中删除“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();
关于java-读写波斯字符串或其他语言而不是英语到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361009/
我使用 Highcharts 绘制一些图表。我的语言是波斯语(波斯语),我想用波斯数字绘制图表。但是在 Highcharts 中,所有数字都以英文显示。 有没有办法用波斯语显示数字(所有数字:yaxi
我是一名优秀的程序员,十分优秀!