目录列表没有刷新

时间:2010-03-26 19:26:36

标签: java java-io

这让我抓狂!我有一个面板,显示目录中的文件列表。该列表存储在矢量中。当我单击一个按钮时,会在同一目录中创建一个新文件,并且需要刷新列表。

我不明白为什么Java无法看到新文件,即使我在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应该出现在其中,然后它将被阅读并显示在面板中。

2 个答案:

答案 0 :(得分:0)

老实说,你的代码很乱。

您从/myFileList.txt读取并且对您所阅读的内容不执行任何操作,除非将其存储在临时Vector中。充其量,这没有效果;在最坏的情况下(例如,如果文件不存在),它会抛出异常。无论它做什么,它都不会创建新文件。

此外,我看不到GUI中可能显示文件列表的面板的引用。您希望它如何更新?

答案 1 :(得分:0)

这对我有用: 要刷新目录列表,请再次调用.listFiles()。

filesFound = fLocation.listFiles(); 应显示最新的目录列表。希望这会对你有所帮助。