-1

後でいくつかのファイルをコピーするフォルダー名をユーザーに尋ねる単純なアプリを作成しました。問題は、フォルダー名に非レーティング (ギリシャ語) 文字が含まれることです。そして、フォルダーが正しい名前でエラーなしで作成されている間、その絶対パスを文字列に保存すると、ギリシャ文字は次のようになります?????? _2012 年 3 月 22 日。保存されたパスを使用してコピーしたファイルを送信しようとすると、Java がパスを正しく読み取れないため、エラーが発生します。

package newOrderAndXCopy;

import java.io.File;
import javax.swing.JOptionPane;
import initiate.*;

public class NewOrder {

private String orderPath = null;

//constructor
public NewOrder() {     

    if(newOrderName()) {

        File nO = new File(orderPath);
        nO.mkdir();

    }       

}

public boolean newOrderName() {
    boolean name = false;
    int counter = 3;
    while(counter > 0) {

        String test = JOptionPane.showInputDialog("Here I ask the user to give the order name with this form -> ΠΑΡΑΛΑΒΗ ΧΧ-ΧΧ-ΧΧΧΧ (π.χ. ΠΑΡΑΛΑΒΗ 12-04-2013):");
        if(!test.matches("ΠΑΡΑΛΑΒΗ \\d{2}-\\d{2}-\\d{4}")) {

            JOptionPane.showMessageDialog(null, "Wrong name!", "Error", JOptionPane.ERROR_MESSAGE);
            counter--;

        }
        else {
                            //replace the space with underscore
            String rep = Config.savesPath + test.replaceAll(" ", "_") + "/";

            File no = new File(rep);
            if(!no.exists()) {
                orderPath = rep;

                --> Config.orderPath = no.getAbsolutePath();  <--
                /*This part is where it gets messy. The folder is created but this value is wrong so I can't use it later!*/
                name = true;
                JOptionPane.showMessageDialog(null, "The order folder was created!!", "Success!", JOptionPane.INFORMATION_MESSAGE);
                break;
            }
            else {
                JOptionPane.showMessageDialog(null, "The order with this name already exists!Pick another Name!", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }

    }
    return name;
}
}
4

2 に答える 2

0

アプリは非ラテン文字を処理する必要があるため、デフォルトのエンコーディングがこれをサポートしていることを確認してください。

System.setProperty("file.encoding", "UTF-8");
于 2016-07-27T02:50:17.483 に答える
0

この答えを見てください。

String encoding = "UTF-8";
new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), encoding ))
new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding ))

問題は、FileWriter を使用するか、encoding パラメータを省略したことにある可能性があります (デフォルトはプラットフォームのエンコーディングです)。

ファイルを読み取るには、エディターでエンコーディングを正しく設定する必要があります。または、文字セットを示すことができる HTML を記述します。

于 2013-04-25T14:16:24.583 に答える