Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

following the question I asked before How to have my java project to use some files without using their absolute path? I found the solution but another problem popped up in creating text files that I want to write into.here's my code:

private String pathProvider() throws Exception {
    //finding the location where the jar file has been located
    String jarPath=URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
    //creating the full and final path
    String completePath=jarPath.substring(0,jarPath.lastIndexOf("/"))+File.separator+"Records.txt";
    
    return completePath;
}

public void writeRecord() {
    
    try(Formatter writer=new Formatter(new FileWriter(new File(pathProvider()),true))) {
        
        writer.format("%s  %s  %s  %s  %s  %s  %s  %s  %n", whichIsChecked(),nameInput.getText(),lastNameInput.getText()
                ,idInput.getText(),fieldOfStudyInput.getText(),date.getSelectedItem().toString()
                ,month.getSelectedItem().toString(),year.getSelectedItem().toString());
        
        successful();
        
    } catch (Exception e) {
        failure();
    }
}

this works and creates the text file wherever the jar file is running from but my problem is that when the information is been written to the file, the numbers,symbols, and English characters are remained but other characters which are in Persian are turned into question marks. like: ????? 111 ????? ????.although running the app in eclipse doesn't make this problem,running the jar does. Note:I found the code ,inside pathProvider method, in some person's question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
122 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your pasted code and the linked question are complete red herrings - they have nothing whatsoever to do with the error you ran into. Also, that protection domain stuff is a hack and you've been told before not to write data files next to your jar files, it's not how OSes (are supposed to) work. Use user.home for this.

There is nothing in this method that explains the question marks - the string, as returned, has plenty of issues (see above), but NOT that it will result in question marks in the output.

Files are fundamentally bytes. Strings are fundamentally characters. Therefore, when you write code that writes a string to a file, some code somewhere is converting chars to bytes.

Make sure the place where that happens includes a charset encoding.

Use the new API (I think you've also been told to do this, by me, in an earlier question of yours) which defaults to UTF-8. Alternatively, specify UTF-8 when you write. Note that the usage of UTF-8 here is about the file name, not the contents of it (as in, if you put persian symbols in the file name, it's not about persian symbols in the contents of the file / in the contents you want to write).

Because you didn't paste the code, I can't give you specific details as there are hundreds of ways to do this, and I do not know which one you used.

To write to a file given a String representing its path:

Path p = Paths.get(completePath);
Files.write("Hello, World!", p);

is all you need. This will write as UTF_8, which can handle persian symbols (because the Files API defaults to UTF-8 if you specify no encoding, unlike e.g. new File, FileOutputStream, FileWriter, etc).

If you're using outdated APIs: new BufferedWriter(new OutputStreamWriter(new FileOutputStream(thePath), StandardCharsets.UTF-8) - but note that this is a resource leak bug unless you add the appropriate try-with-resources.

If you're using FileWriter: FileWriter is broken, never use this class. Use something else.

If you're converting the string on its own, it's str.getBytes(StandardCharsets.UTF_8), not str.getBytes().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...