-1

我制作了一个简单的应用程序,询问用户一个文件夹名称,稍后将在其中复制一些文件。问题是文件夹名称将包含非翻译(希腊语)字符。虽然使用正确的名称创建文件夹并且没有错误,但当我将其 AbsolutePath 存储在字符串中时,希腊字符会变成这样?????? _22-03-2012。当我尝试使用存储的路径发送复制的文件时出现错误,因为 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

看看这个答案

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

问题可能在于使用 FileWriter 或省略了编码参数(默认为平台编码)。

要读取文件,您需要在编辑器中正确设置编码。或者在可以指示字符集的地方编写 HTML。

于 2013-04-25T14:16:24.583 回答
0

您的应用需要处理非拉丁字符,因此请确保默认编码支持这一点。

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