2016-05-05 6 views
1

私はいくつかのファイルを含む1つのフォルダを持っています。接頭辞 'Rz'と接尾辞で始まるすべてのファイルが必要です。 lst。 。 RzCountryのために。 Lst。私は...国をしたいので、私はどのように行うことができます。ここJavaでPrefixfilefilterとsuffixfilefilterを使用してファイルを取得する

+1

あなたのこれまでに試してみましたか? – tfv

+0

私はフィルタを使って試しました。 –

答えて

0

はあなたが必要なもののexempleであること:

public static void main(String[] args) { 

// replace here by your directory path 
File dir = new File("directory_path"); 

String prefixe = "Rz"; 
String sufixe = "ls"; 

// retrieve all the file with name containing Rz at the beginning and ends with ls 
File[] files = dir.listFiles(fileName -> 
        fileName.getName().startsWith(prefixe) && 
        fileName.getName().endsWith(sufixe)); 

// if you need to have all the name of the file without prefixe/sufixe 
List<String> list = Stream.of(files) 
          .map(file -> 
           file.getName().substring(prefixe.length(), 
                 file.getName().length() - sufixe.length())) 
          .collect(Collectors.toList()); 
} 
+0

私のために働いていません。それは配列の全体の経路を取っている..私は国だけを表示したい。 –

+0

こんにちは、検索するファイルが入っているフォルダのパスでdirectory_pathを置き換える必要があります。この行を使ってすべての値を表示することができます:list.forEach(System.out :: println); –

関連する問題