开发者

Directory listing not refreshing

This is driving me crazy! I have a panel that displays a list of files from a directory. The list is stored in a vector. When I click on a button, a new file is created in the same directory and the list needs to be refreshed.

I don't understand why Java cannot see the new file, even though if I add a good old DIR in Dos, Dos can see the file. It's as if the new file is invisible even though I can see it and Dos can see it. I tried giving it some time (sleep, yield) but it's no use. I also tried copying to a new temp file and reading the temp but again to no avail. Here's some code (removed some irrelevant lines):

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
开发者_如何学Go         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());
       }
 }

}

I would appreciate any leads. Thank you.

myFileList.txt lokks like this:

myline1
myline2
myline3

After adding a new file in the folder,

myline4 should appear in it, then it will be read and displayed in the panel.


Honestly, your code is a mess.

You read from /myFileList.txt and do nothing with what you read, except store it in a temporary Vector. At best, this has no effect; at worst (if the file doesn't exist, for example) it throws an exception. Whatever it does, it does not create a new file.

Furthermore, I see no reference to the panel in your GUI that supposedly displays the file list. How do you expect it to get updated?


This works for me: To refresh the directory list, call .listFiles() again.

filesFound = fLocation.listFiles(); should show the most updated directory listing. Hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜