2015-01-19 3 views
0

C : /file.txt에 저장된 파일이 하나 있습니다. 속성 파일 location.properties에는 경로 : C : /file.txt 만 포함되어 있습니다. 속성 파일을 읽고 위치를 얻고 파일을 읽고 모든 것을 표시하려고합니다. 하지만 fileNotFound 예외가 발생합니다. 아무도 나를 도울 수 있습니까? 유효한 경로가 아닙니다 인 ("C:\file.txt"을 열려고하는, 그래서속성 파일의 위치를 ​​사용하여 파일 읽기

"C:\file.txt" 
java.io.FileNotFoundException: "C:\file.txt" (The filename, directory name, or volume label syntax is incorrect.) 
    at java.io.FileInputStream.<init>(FileInputStream.java:156) 
    at java.io.FileInputStream.<init>(FileInputStream.java:111) 
    at java.io.FileReader.<init>(FileReader.java:69) 
    at com.tcs.fileRead.ReadFile.main(ReadFile.java:29) 
+2

경로의 경로가 따옴표 인 이유는 무엇입니까? 따옴표는 Windows의 경로에서는 유효하지 않습니다 (가능한 경우에도 다른 플랫폼에서는 사용되지 않습니다). – immibis

+0

감사합니다. 내가 잘못한 속성 파일에 ""을 넣었습니다. :) @immibis – Ajit

답변

1

당신은 당신의 재산에 따옴표로 둘러싸인 경로가 파일을했다 :

package com.tcs.fileRead; 

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Properties; 

public class ReadFile { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Properties prop = new Properties(); 

     try { 

      prop.load(new FileInputStream("location.properties")); 
      //prop.load(fileIn); 
      String loc = prop.getProperty("fileLoc"); 
      System.out.println(loc); 

      BufferedReader buffer; 
      buffer = new BufferedReader(new FileReader(loc)); 
      String line; 
      while((line =buffer.readLine())!= null) 
      { 
       System.out.println(line); 
      } 



     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 

} 

이것은 출력이 내 코드입니다) 대신 C:\file.txt입니다.

관련 문제