2012-05-14 37 views
2

我需要製作一個列出目錄中的.txt文件的菜單。例如,如果我有jonsmith12.txt,lenovo123.txt,dell123.txt目錄將如何使我的ArrayList菜單:ArrayList菜單Java

請選擇下列之一:

  1. jonsmith12
  2. lenovo123
  3. dell123

請輸入您的選擇:

我需要一個ARR aylist菜單是因爲我不知道在任何給定時間在目錄中有多少.txt文件。

import java.io.File; 

public class ListFiles 
{ 

    public static void listRecord() { 

    // Directory path here 
    String path = "."; 

    String files; 
    File folder = new File(path); 
    File[] listOfFiles = folder.listFiles(); 

    for (int i = 0; i < listOfFiles.length; i++) 
    { 

    if (listOfFiles[i].isFile()) 
    { 
    files = listOfFiles[i].getName(); 
     if (files.endsWith(".txt") || files.endsWith(".TXT")) 
     { 
      System.out.println(files); 
     } 
    } 
    } 
} 
} 

這裏是將.txt文件中的信息顯示到控制檯上的類。它仍然需要一些修改,但我可以弄清楚。

import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

/** 
* This program reads a text file line by line and print to the console. It uses 
* FileOutputStream to read the file. 
* 
*/ 
public class DisplayRec { 

    public static void displayRecord() throws IOException { 

    File file = new File("williamguo5.txt"); 
    FileInputStream fis = null; 
    BufferedInputStream bis = null; 
    DataInputStream dis = null; 

    try { 
     fis = new FileInputStream(file); 

     // Here BufferedInputStream is added for fast reading. 
     bis = new BufferedInputStream(fis); 
     dis = new DataInputStream(bis); 

     // dis.available() returns 0 if the file does not have more lines. 
     while (dis.available() != 0) { 

     // this statement reads the line from the file and print it to 
     // the console. 
     System.out.println(dis.readLine()); 
     } 

     // dispose all the resources after using them. 
     fis.close(); 
     bis.close(); 
     dis.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

所以現在的問題是:如何實現ArrayList菜單到我ListFiles類,所以它會顯示.TXT文件。

+0

問題是什麼? – dj18

+0

如何在我的ListFiles類中實現ArrayList菜單,以顯示.txt文件。 –

+4

如果這是作業,請標記爲這樣。現在,你有什麼嘗試?什麼沒有用?你甚至還有菜單類/方法嗎? – dj18

回答

0

它看起來像你上面的堅持點是陣列。如果您只需要遍歷目錄中的文件,那麼像下面這樣簡單的操作就可以實現。

import java.io.File; 


public class TxtEnumerator { 
    public static void main(String[] args) { 
     TxtEnumerator te = new TxtEnumerator(); 
     te.listFiles(); 
    } 

    public void listFiles() { 
     String filepath = "." + File.separator + "textDirectory"; 
     File file = new File(filepath); 
     if (file.isDirectory()) { 
      for (File f : file.listFiles()) { 
       if (f.getName().endsWith(".txt")) { 
        System.out.println(f.getName()); 
       } 
      } 
     } 
    } 
} 
1

您可以使用另一種方法簽名File.listFiles(FilenameFilter filter)簡化代碼:

File[] files = dir.listFiles(new FilenameFilter() { 

    @Override 
    public boolean accept(File dir, String name) { 
     return name.toLowerCase().endsWith(".txt"); 
    } 
}); 

,除非你真的喜歡寫循環,你甚至都不需要在陣列上手動循環轉換它的List

List<File> lstRecords = Arrays.asList(files); 

displayRecord方法是很接近;您只需將該文件作爲參數傳遞並使用該文件而不是硬編碼的文件名,並且您需要初始化dis

全部放在一起:

package com.example.file; 

import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FilenameFilter; 
import java.io.IOException; 
import java.util.Arrays; 
import java.util.List; 

public class FileExample { 

    public static List<File> listRecords(File dir) { 
     File[] files = dir.listFiles(new FilenameFilter() { 

      @Override 
      public boolean accept(File dir, String name) { 
       return name.toLowerCase().endsWith(".txt"); 
      } 
     }); 

     return Arrays.asList(files); 
    } 

    public static void displayRecord(File file) { 
     FileInputStream fis = null; 
     BufferedInputStream bis = null; 
     DataInputStream dis = null; 

     try { 
      fis = new FileInputStream(file); 

      // Here BufferedInputStream is added for fast reading. 
      bis = new BufferedInputStream(fis); 
      dis = new DataInputStream(bis); 

      String line = dis.readLine(); 
      while (line != null) { 

       // this statement reads the line from the file and print it to 
       // the console. 
       System.out.println(line); 
       line = dis.readLine(); 
      } 

      // dispose all the resources after using them. 
      fis.close(); 
      bis.close(); 
      dis.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     List<File> lstRecords = listRecords(new File(".")); 
     for (File record : lstRecords) { 
      displayRecord(record); 
     } 
    } 

} 

它也更好地使用讀取/寫入器代替的InputStream/OutputStream中,如果你使用文本文件,你應該關閉在finally塊的文件,以避免潛在的資源泄漏。

您還會注意到我沒有明確使用ArrayList。在大多數情況下,最好儘可能在接口上進行編程(在本例中爲List),並且只在需要使用僅適用於該類的方法時使用實現類聲明變量。

+0

+1很好的建議在這裏... –