Mithilesh Kumar
Mithilesh Kumar

Reputation: 102

I am going to write file (logs) on server in deferent languages. english_name.txt is ok, "عاتبوها.txt" is showing "?????.txt" on server

Going to log on server in differant languages say "عاتبوها.txt" but it is showing "?????.txt" on server.

Code to write logfile on sever below :

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

Now the Log generating is ok on server and data on log file is also ok in a file... but the problem is fileName is not generate in another language like arebic.txt, its shown ??????????_.txt... please help.

Upvotes: -3

Views: 103

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109613

First a minor issue the java source must be edited in the same encoding (charset) as the java compiler compiles, in order to correctly translate Arabic text in the java source. This can be tested by comparing a string literal in Arabic with a string literal with u-escaped Arabic: "\u....\u....".

Here there are two problems: the file name and the file contents. For the file name check the afore mentioned minor issue. If that does not help, you probably need to run the application server in a UTF-8 locale; look into Linux help.

Under Windows the filename indeed does not work at my place. You could write a zip file with latin characters, and have UTF-8 filenames in it.

The file contents must be written using a conversion so the resulting bytes are in UTF-8 (or for Arabic Windows-1256). Here FileWriter/FileReader are unusable, as they use the default encoding and one cannot specify the encoding as with other classes.

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

By the way, if the text is for Windows, use \r\n (CR-LF) instead of \n (LF).

Upvotes: 0

Related Questions