Java reading and writing Persian strings or other language than English to text files

I need code with any java.io package class or other package if available, read and write lines like "سلام به همه دوستانم" (Persian) from the file. but I haven't been able to find a good way to do this. the problem is saving the file: does it show? after that in a program like notepad Thank you all here is the code I tried:

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;
}

      

+3


source to share


2 answers


Remove "UTF-8" from the streams and put it on a string to be written to the file
Remember that spelling English with Arabic or Persian characters sometimes causes some conflict



   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


source


1 - the trick is stored in Arabic and displayed in Persian

change text from arabic to farsi

text.trim().replaceAll("ی", "ي").replaceAll("ک", "ك");

      



change text from Farsi to Arabic

text.trim().replaceAll("ي", "ی").replaceAll("ك","ک");

      

2-other way is to save in base64 format and save it

0


source







All Articles