2013-08-28 113 views
0

我試圖讀取從屬性讀取的文件路徑中的文件,但我不斷收到FileNotFoundException(該文件存在)。從屬性文件讀取文件路徑

test.properties:

test.value = "src/main/resources/File.csv" 

LoadProperties.java:

public class LoadProperties { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 

     Properties aProp = new Properties(); 
     aProp.load(new FileInputStream("src/main/resources/test.properties")); // works 

     String filepath = aProp.getProperty("test.value"); 
     System.out.println(filepath); // outputs: "src/main/resources/File.csv" 

     FileReader aReader = new FileReader("src/main/resources/File.csv"); // works 
     FileReader aReader2 = new FileReader(filepath); // java.io.FileNotFoundException 
    } 
} 

,而它上面的行工作得很好,爲什麼被拋出此異常? 如何從屬性提供的路徑中讀取文件?

回答

2

你不應該把「在你的屬性文件在這裏的Java將其視爲:。

String file = "\"src/main/resources/File.csv\""; 
0
test.value =src/main/resources/File.csv 

你並不需要在性能雙引號文件來表示一串連續的

0

您可以編寫自己的邏輯來讀取屬性文件,文件路徑中是否存在單引號或雙引號並不重要

String propertyFileLocation = "C:\a\b\c\abc.properties"; 
try 
    { 
     fileInputStream = new FileInputStream(propertyFileLocation); 
     bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); 
     properties = new Properties(); 
     String currentLine = null; 
     String[] keyValueArray = null; 
     while ((currentLine = bufferedReader.readLine()) != null) { 
      if (!currentLine.trim().startsWith("#")) { 
       keyValueArray = currentLine.split("="); 
       if (keyValueArray.length > 1) { 
        properties.put(keyValueArray[0].trim(), keyValueArray[1].trim().replace("\\\\","\\")); 
       } 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     return null; 
    }