2016-12-28 2 views
-2

私のコードは基本的なファイル作成者とマネージャです。すべてのファイルを整理する主な方法は、ディレクトリのテキストファイルです。プログラムにアクセスできるように配列にインポートします。私の削除ボタンは、ArrayListから文字列を削除しません。どうすればこの問題を解決できますか?私のプログラムがArrayListから文字列を削除しないのはなぜですか

//This part initialises the code and imports the array 
private void startActionPerformed(java.awt.event.ActionEvent evt) { 
    ArrayList<String> directory = new ArrayList(); 
    String content1; 

    try { 
     content1 = new String(Files.readAllBytes(Paths.get("directory.txt"))); 

     output.setText(content1); 
     directory.add(content1); 

     refresh(); 
    } catch (IOException ex) { 
     Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

//This allows me to refresh the output text area 
private void refresh() { 
    String content = ""; 

    for (int i = 0; i < directory.size(); i++) { 
     content = content + directory.get(i) + "\n"; 
    } 
    try { 
     Files.write(Paths.get("directory.txt"), directory); 
    } catch (IOException ex) { 
     Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    output.setText(content); 
    System.out.println(directory); 
} 

//This deletes the file from the directory and the actual file 
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {          
    directory.remove(input.getText()); 
    String fileDelete = input.getText(); 
    directory.remove(input.getText()); 
    Path deletefile = (Paths.get(fileDelete + ".txt")); 

    try { 
     Files.delete(deletefile); 
    } catch (IOException ex) { 
     Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    try { 
     Files.write(Paths.get("directory.txt"), directory); 
    } catch (IOException ex) { 
     Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    refresh(); 
}   
+3

メソッド 'startActionPerformed'のスコープ内で、' ArrayList directory = new ArrayList();を定義しました。結果として、このローカルの 'ArrayList'だけが値を保持しますが、そこに決まった' directory'フィールドに決して置かれないので、あなたのディクテーションは何もしなくても動作します。 – SomeJavaGuy

+0

私は同じことを考えていました。範囲の問題がないと確信していますか? – Coop

+0

'ArrayList ディレクトリ= new ArrayList();は' startActionPreformed'の前にあります。私はそれを間違ってここにコピーした。 –

答えて

0

デバッグ時にファイルが開いている可能性があります。プログラムがそれにアクセスできることを確認してください

関連する問題