2013-04-25 149 views
-1

我做了一个简单的应用程序,要求用户输入一个文件夹名称,以便稍后复制一些文件。问题是该文件夹名称将包含非lating(希腊)字符。虽然该文件夹正在创建正确的名称和没有错误,当我将它的AbsolutePath存储在一个字符串希腊字符得到这样的?????? _22-03-2012。所以当我尝试使用存储路径发送复制的文件时,我得到错误,因为java无法正确读取路径!Java文件路径编码?

任何想法? (显然,我是新与Java所以裸跟我PLZ!)

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

这可能是一个显示问题,而不是数据存储在File对象中的问题。你能否展示一些代码来演示你如何生成文件以及如何打印路径? – 2013-04-25 13:44:36

回答

0

this answer

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

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

为了读取文件,您需要在编辑器右侧设置编码。 或者在HTML中指定字符集。

+0

不,正如我所说的文件夹正在创建!问题是,当我将它的absolutePath存储在一个字符串中,以便稍后将它用作目标路径时,它将以非拉丁字符形式存储为问号。但实际创建的文件夹名称是确定的。 – user1712246 2013-04-25 14:22:44

+0

'String test = JOptionPane.showInputDialog'(存储在字符串#1中)仍然可以。某种程度上'Config.savesPath'可能是错误的。你如何得到它?如果来自.properties(ISO-8859-1),应该使用'\ uXXXX'代码。 – 2013-04-25 14:55:09

+0

Config.savesPath没问题,因为它只包含拉丁字符。 Config.targetPath有问题,我通过询问文件的绝对路径来得到它。 – user1712246 2013-04-25 15:02:03

0

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

System.setProperty("file.encoding", "UTF-8");