Я хочу код с любым классом пакета java.io или другого пакета, если он доступен, , чтобы читать и писать строку типа «سلام به همه دوستانم "(персидский язык) из файла. но я не смог найти хороший способ для этого. проблема заключается в сохранении файла: он показывает? после этого в программе, например блокнотом спасибо всем здесь есть код, который я попробовал:java-чтение и запись персидских строк или другого языка, отличного от английского, до текстовых файлов
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;
}
'новый InputStreamReader (System.in , "UTF-8"); 'Вы уверены, что' System.in' закодирован с использованием 'UTF-8'? – Pshemo
Если вы находитесь в Windows, и вы запускаете приложение на консоли, проверьте его кодовую страницу. Вы можете использовать команду 'chcp'. Для меня он возвращает '852', поэтому правильная кодировка для меня - это кодовая страница' cp852', когда я читаю «System.in». – Pshemo