Note that there are some explanatory texts on larger screens.

plurals
  1. PODirectory listing not refreshing
    primarykey
    data
    text
    <p>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. </p> <p>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):</p> <pre><code>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&lt;String&gt;(); 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 &lt; 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()); } } } </code></pre> <p>I would appreciate any leads. Thank you.</p> <p>myFileList.txt lokks like this:</p> <pre><code>myline1 myline2 myline3 </code></pre> <p>After adding a new file in the folder,</p> <p>myline4 should appear in it, then it will be read and displayed in the panel.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload