tags:

views:

44

answers:

1

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
         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.

A: 

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?

Thomas
Some lines of code were edited in order to make the code presented here a bit lighter and protect my company's ip. The vector is in fact in another function and it is read, then the content ends-up in a panel. I agree that it is not clear, but it's the best I can do. There is a util() function that displays to the shell all info (the content of myFileList.txt).
alex
Well, if you cannot share the code that fails, I cannot help you further. Sorry.
Thomas
That's all right. I am looking more to find theoretical suggestions or alternate ways of listing a directory then reading the file. It looks to me as if windows locks the directory index somehow and io cannot read the "latest" version of it. Something weird like that. I think io has issues with locks.
alex