2012-03-31 3 views
3

내가 파일 읽기 시도하고이 오류가 있습니다이상한 예외 "주"java.io.FileNotFoundException I/O 자바

Exception in thread "main" java.io.FileNotFoundException: \src\product.txt (No such file or directory) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:120) 
at dao.Inventory.readFile(Inventory.java:30) 
at view.InventoryView.init(InventoryView.java:33) 
at view.InventoryView.<init>(InventoryView.java:21) 
at view.InventoryView.main(InventoryView.java:211) 

을하지만 것입니다, 나는 product.txt이 내 src 폴더에.

public void readFile() throws IOException { 
      // input file must be supplied in the first argument 
      InputStream istream; 
        File inputFile = new File("\\src\\product.txt"); 
        istream = new FileInputStream(inputFile); 

        BufferedReader lineReader; 
       lineReader = new BufferedReader(new InputStreamReader(istream)); 
        String line; 

         while ((line = lineReader.readLine()) != null) { 

       StringTokenizer tokens = new StringTokenizer(line, "\t"); 

       // String tmp = tokens.nextToken(); 
       // System.out.println("token " + tmp); 
       ActionProduct p = new ActionProduct(); 
       prodlist.add(p); 
       String category = p.getCategory(); 
       category = tokens.nextToken(); 
       System.out.println("got category " +category); 

       int item = p.getItem(); 
       item = Integer.parseInt(tokens.nextToken()); 

       String name = p.getName(); 
       System.out.println("got name " +name); 

       double price = p.getPrice(); 
       price = Double.parseDouble(tokens.nextToken()); 

       int units = p.getUnits(); 
       units = Integer.parseInt(tokens.nextToken()); 
      } 
     } 

난 아무것도 내 코드 문제라고 생각하지 않습니다 :

내 코드는 다음과 같다. 또한 FILE.TXT.TXT와 같은 숨겨진 확장에 대한 유사한 게시물을 보았습니다. 어떻게 MacOSX에서 숨겨진 확장을 표시하겠습니까 ?? 어떤 제안? (숨겨진 확장 문제 이외에 다른 문제가 있습니까?)

+3

당신은'src에 \\ product.txt' 경로로해야한다. –

+3

'File inputFile = new File ("./ src/product.txt"); 대신 사용해보십시오. –

+0

또는 파일 inputFile = 새 파일 ("src \\ product.txt"); – nikhil

답변

6

/src/product.txt 절대 경로는 ..\\src\\product.txt

참조를 시도 할 수 루트 경로 (/)의 src 폴더에 있습니다. src/product.txt을 사용하면 프로그램에서이를 상대 경로로 사용합니다. 다른 댓글이 언급 한 바와 같이

+1

+1 \ 대신 \를 사용하거나 독립적 인 운영 체제의 경우'/'를 사용하십시오. –

+0

@ Eng.Fouad 감사합니다, 내 대답을 –

+0

업데이트했습니다. 내가 자바에 새로운 사람과 같은 문제에 직면 해, 대상이 src가 아닌 파일이 있다면? 나는이 모든 시도했지만 여전히 –

1

Java 코드가 src의 상위 폴더 내에서 실행되는 것이 아니라 'class'또는 'bin' '폴더에 컴파일 된 java .class 파일이 있습니다. 프로그램이 파일을 찾을 것을 시도 할 것이다, 그래서 'SRC'와 '빈'은 같은 디렉토리에 있다고 가정

, 당신은 또한 http://en.wikipedia.org/wiki/Path_(computing)

1
  1. 경로는 절대 및 포인트 \ SRC \ product.txt (희망)하지 당신의 소스가 어디에 저장되어된다.

  2. 경로 구분 기호는 System.getProperty ("path.separator") 속성을 사용하여 OS 독립적 방식으로 설정해야합니다. Unix 시스템에서는 하드 코드 된 백 슬래시를 경로 분리 자로 사용하는 데 문제가 있습니다. 휴대용! 더 나은 아직

    String pathSeparator = System.getProperty("path.separator"); 
    String filePath = "." + pathSeparator + "src" + pathSeparator + "product.txt"; 
    File file = new File(filePath); 
    

    또는

: 파일이`src` 폴더에있는 경우

// this could reside in a non-instantiable helper class somewhere in your project 
public static String getRelativePath(String... pathElements) { 
    StringBuilder builder = new StringBuilder("."); 
    for (String pathElement : pathElements) { 
     builder.append(System.getProperty("path.separator"); 
     builder.append(pathElement); 
    } 
    return builder.toString(); 
} 

// this is where your code needs a path 
... 
new File(getRelativePath("src", "product.txt"); 
...