2010-03-26 5 views
0

이것은 나를 미치게합니다! 디렉토리의 파일 목록을 표시하는 패널이 있습니다. 목록은 벡터에 저장됩니다. 버튼을 클릭하면 같은 디렉토리에 새 파일이 만들어지고 목록을 새로 고쳐야합니다.디렉토리 목록이 새로 고쳐지지 않습니다

Dos에서 좋은 이전 DIR을 추가해도 Dos에서 파일을 볼 수 있지만 자바가 새 파일을 볼 수없는 이유를 모르겠습니다. 마치 새 파일을 볼 수 있고 Dos에서 볼 수는 있어도 새 파일이 보이지 않는 것입니다. 나는 약간의 시간 (수면, 수확량)을 주려고 노력했지만 아무 쓸모가 없다. 나는 또한 새로운 임시 파일에 복사하고 임시 읽기를 시도했지만 아무 소용이 없습니다. 다음은 일부 코드입니다 (관련없는 일부 선을 제거함).

public class Button extends EncapsulatedButton { 

public Button() 
{ 
    super("button pressed"); 
} 

public void actionPerformed(ActionEvent arg0) { 

//removed function here where the new file is created in the directory 
//remove call to DOS that regenerates /myFileList.txt after a new file was added in the directory 
//at this point, DOS can see the new file and myFileList.txt is updated, however it is read by java without the update!!!!! 

//now trying to read the directory after the new file was created 

    Vector data = new Vector<String>(); 
    String s = null; 

// Create the readers to read the file. 

    try { 
    File f = new File("/myFileList.txt"); 
    BufferedReader stream = new BufferedReader(new InputStreamReader(new FileInputStream(f))); 

    while((s = stream.readLine()) != null) 
    { 
    data.addElement(s.trim()); 
    } 
    stream.close(); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } 

    util(); 

} 

void util(){ 
//giving it time is not helping 
    Thread.yield(); 
    try { 
    Thread.sleep(10000); 
    } catch (InterruptedException e1) { 
    e1.printStackTrace(); 
    } 
    //get the file listing through java instead of DOS - still invisible 
    File fLocation = new File("/myDir"); 
    File[] filesFound = fLocation.listFiles(); 

    for (int i = 0; i < filesFound.length; i++) { 
     if (filesFound[i].isFile()) { 
     System.out.println("**********" + filesFound[i].getName()); 
     } 
    } 

//last resort: copy to a temp then read from there - still not good 
    try{ 
    //copy to a temp file 
     File inputFile = new File("/myFileList.txt"); 
     File outputFile = new File("/myFileList_temp.txt"); 

     FileReader in = new FileReader(inputFile); 
     FileWriter out = new FileWriter(outputFile); 
     int c; 

     while ((c = in.read()) != -1) 
     out.write(c); 

     in.close(); 
     out.close(); 

    //read the copy to see if it is updated 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream("/myFileList_temp.txt"); 
     // Get the object of DataInputStream 
     DataInputStream in1 = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in1)); 
     String strLine; 
     //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println ("Test file read: " + strLine); 
     } 
     //Close the input stream 
     in1.close(); 
     }catch (Exception e){//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
     } 
} 

} 

나는 모든 리드를 부탁드립니다. 고맙습니다. 이 같은

myFileList.txt의 lokks은 :

myline1 
myline2 
myline3 

myline4가에 표시, 폴더에 새 파일을 추가 한 후, 다음 판독되고 패널에 표시됩니다.

+0

정확히이 파일을 읽으려고하십니까? 정말 루트 디렉토리?/myDir은 무엇입니까? – FRotthowe

+0

파일과 디렉터리는 런타임에 동적으로 생성됩니다. 여기에 제시된 위치는 로컬 디렉토리의 더미 대체품입니다. 코드베이스는 꽤 복잡하지만 좋은 근사값이라고 생각했습니다. 어쨌든 잘못된 파일이나 잘못된 디렉토리를 읽는 경우는 아닙니다. 텍스트 편집기에서 열리는 파일을 볼 수 있으며 새로운 줄이 추가되었습니다. – alex

+0

왜 하나님의 이름으로, 단순히 디렉토리 내용을 나열하는 파일 대신에 File 클래스에이 방법이있는 디렉토리를 읽지 않습니까?) – Ingo

답변

0

솔직히 말해서, 코드가 엉망입니다.

/myFileList.txt에서 읽었을 때 읽은 내용으로 아무 것도하지 않습니다. 단 Vector에 저장하십시오. 기껏해야, 이것은 효과가 없습니다. 최악의 경우 (예를 들어, 파일이 존재하지 않으면) 예외가 발생합니다. 그것이 무엇이든, 그것은 새로운 파일을 생성하지 않습니다.

또한 GUI에서 파일 목록을 표시하는 패널에 대한 참조가 없습니다. 어떻게 업데이 트가 기대됩니까?

+0

여기에 제시된 코드를 좀 더 가볍게 만들고 회사의 IP를 보호하기 위해 일부 코드 줄이 편집되었습니다. 벡터는 실제로 다른 기능에 있으며 읽히고 내용은 패널에서 끝납니다. 나는 그것이 명확하지 않다는 데 동의하지만, 내가 할 수있는 최선이다. 쉘에 모든 정보 (myFileList.txt의 내용)를 표시하는 util() 함수가 있습니다. – alex

+0

실패한 코드를 공유 할 수 없다면 더 이상 당신을 도울 수 없습니다. 죄송합니다. – Thomas

+0

괜찮습니다. 나는 이론적 인 제안이나 디렉토리를 열거하고 파일을 읽는 방법을 찾기 위해 더 많은 것을 찾고있다. Windows가 디렉토리 색인을 어떻게 든 잠그고 io가 "최신"버전을 읽을 수없는 것처럼 보입니다. 그런 이상한 것. 나는 io가 자물쇠에 문제가 있다고 생각합니다. – alex

0

나를 위해 : 디렉토리 목록을 새로 고치려면 .listFiles()를 다시 호출하십시오.

filesFound = fLocation.listFiles(); 에는 가장 최신 디렉토리 목록이 표시되어야합니다. 희망이 당신을 도와줍니다.

관련 문제