2013-04-02 2 views
0

프로젝트를 빌드했습니다. 프로젝트는 구성 파일과 데이터 파일 (두 텍스트 파일 모두)을 읽어야합니다. 사용자가 업로드 한 파일은 다른 이름을 가질 수 있습니다. 모든 것이 작동합니다.jar 외부에서 파일 읽기

프로젝트에서 runnable jar를 생성하고 다음과 같이 cmd에서 실행하려고했습니다 : java -jar walksat.jar "params_3.txt", "Test3ArielWCNF-log.wcnf". jar 파일의 인수를 인쇄 할 수 있지만 확인 결과는 다음과 같습니다. java.io.FileNotFoundException: params_3.txt, (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at mla.project.main.WalkSAT.setGlobalParams(WalkSAT.java:347) at mla.project.main.WalkSAT.main(WalkSAT.java:60) 두 번째 파일에 대해 동일한 오류가 발생합니다. 하지만 파일의 이름을 하드 코딩하면 (args [0]과 args [1] 대신) jar에서 읽을 수 있습니다.

jar 파일과 다른 두 파일은 같은 폴더에 있습니다.

무엇이 여기에 있습니까?

업데이트args[0]은 결국 ,입니다. 나는 그것을 보지 못했습니다.

+0

[0]'와'인수 [1]', 그들이 같이 표시합니까? –

+0

구성 및 데이터 파일 이름. –

+0

정확하게 출력 해주세요. –

답변

2
File jarFile = new File("."); 

이렇게하면 jar가있는 현재 디렉토리에 대한 참조를 얻을 수 있습니다. 이제 jarfile.listFiles()를 사용하여이 파일을 읽을 수 있습니다. 구성, 데이터 파일 및 jar 파일을 같은 폴더에 보관해야합니다.

runnable jar로 내보내고 실행할 수있는 작은 주 클래스를 만들었습니다. 디렉토리에있는 모든 파일을 나열합니다.

import java.io.File; 당신은`인수를 인쇄 할 때

공용 클래스 리더는 {

public static void main(String[] args) { 

     File jarFile = new File("."); 
     if(jarFile.isDirectory()) 
     { 
      File[] allFiles = jarFile.listFiles(); 
      System.out.println("Printing File names"); 
      for (File file : allFiles) { 
       System.out.println(file.getName()); 
       //code for reading the file goes here.............. 
      } 
     } 

    } 

은}

관련 문제