0

これは私を夢中にさせています!ディレクトリからのファイルのリストを表示するパネルがあります。リストはベクトルに保存されます。ボタンをクリックすると、同じディレクトリに新しいファイルが作成され、リストを更新する必要があります。

Dosに古き良きDIRを追加しても、Dosはファイルを見ることができますが、Javaが新しいファイルを見ることができない理由がわかりません。私はそれを見ることができ、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は次のようになります。

myline1
myline2
myline3

フォルダに新しいファイルを追加した後、

myline4が表示されたら、それが読み取られてパネルに表示されます。

4

2 に答える 2

0

これは私にとってはうまくいきます。ディレクトリリストを更新するには、.listFiles() を再度呼び出します。

filesFound = fLocation.listFiles(); 最新のディレクトリ リストが表示されます。これがお役に立てば幸いです。

于 2014-01-18T19:11:47.177 に答える
0

正直なところ、あなたのコードはめちゃくちゃです。

一時的に保存することを除いて、読んだものを読ん/myFileList.txtで何もしませんVector。せいぜい、これは何の効果もありません。最悪の場合 (ファイルが存在しない場合など)、例外がスローされます。何をしても、新しいファイルは作成されません。

さらに、ファイルリストを表示すると思われるGUIのパネルへの参照がありません。どのように更新されると思いますか?

于 2010-03-26T19:39:29.850 に答える