Swing/RecursiveFileSearch のバックアップ差分(No.23)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/RecursiveFileSearch へ行く。
- 1 (2004-02-26 (木) 02:48:56)
- 2 (2004-06-02 (水) 09:58:45)
- 3 (2004-08-31 (火) 12:30:02)
- 4 (2004-10-08 (金) 06:24:24)
- 5 (2004-11-04 (木) 10:10:11)
- 6 (2005-02-03 (木) 02:04:18)
- 7 (2005-04-28 (木) 04:32:38)
- 8 (2005-11-01 (火) 00:28:25)
- 9 (2006-02-27 (月) 16:19:15)
- 10 (2006-04-28 (金) 20:52:13)
- 11 (2007-02-22 (木) 17:59:19)
- 12 (2007-09-21 (金) 12:23:06)
- 13 (2008-07-11 (金) 15:32:05)
- 14 (2012-08-14 (火) 17:38:50)
- 15 (2012-08-14 (火) 19:55:06)
- 16 (2013-02-26 (火) 14:45:43)
- 17 (2013-02-26 (火) 15:58:32)
- 18 (2013-09-28 (土) 21:41:14)
- 19 (2014-05-22 (木) 14:32:23)
- 20 (2014-10-15 (水) 01:52:26)
- 21 (2014-11-25 (火) 16:07:06)
- 22 (2015-03-09 (月) 14:46:02)
- 23 (2015-03-16 (月) 17:28:33)
- 24 (2016-01-01 (金) 01:03:42)
- 25 (2017-04-04 (火) 14:17:08)
- 26 (2017-04-07 (金) 13:51:51)
- 27 (2017-06-15 (木) 15:19:56)
- 28 (2017-08-24 (木) 17:39:46)
- 29 (2018-02-23 (金) 21:36:29)
- 30 (2019-04-19 (金) 13:43:27)
- 31 (2020-03-05 (木) 17:19:24)
- 32 (2021-08-07 (土) 14:36:24)
- 33 (2024-02-02 (金) 12:02:24)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: Fileの再帰的検索 tags: [File, JProgressBar, SwingWorker] author: aterai pubdate: 2003-12-15 description: 指定したDirectory以下のFileを再帰的に検索し、その進捗状況をJProgressBarで表示します。 --- * 概要 [#la10adfb] 指定した`Directory`以下の`File`を再帰的に検索し、その進捗状況を`JProgressBar`で表示します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRh7du1II/AAAAAAAAAhU/jcMUoOTcbTU/s800/RecursiveFileSearch.png) * サンプルコード [#n78bba98] #code(link){{ private void recursiveSearch(File dir, final List<File> list) throws InterruptedException { for (String fname : dir.list()) { if (Thread.interrupted()) { throw new InterruptedException(); } File sdir = new File(dir, fname); if (sdir.isDirectory()) { recursiveSearch(sdir, list); } else { scount++; if (scount % 100 == 0) { publish(new Message("Results:" + scount + "\n", false)); } list.add(sdir); } } } }} * 解説 [#l9b71aa9] 上記のサンプルでは、選択したフォルダ以下のファイルを再帰的にすべて検索して表示するようになっています。 ---- `JProgressBar`を使った進捗状況の表示とキャンセルには、`SwingWorker`を利用しています。 ---- `JDK 1.7.0`以上の場合は、`Files.walkFileTree(...)`などを使用する方法もあります。 - [http://docs.oracle.com/javase/jp/7/api/java/nio/file/Files.html#walkFileTree(java.nio.file.Path,%20java.nio.file.FileVisitor) Files#walkFileTree(...) (Java Platform SE 7)] - [http://docs.oracle.com/javase/tutorial/essential/io/walk.html Walking the File Tree (The Java™ Tutorials > Essential Classes > Basic I/O)] #code{{ private void recursiveSearch(Path dir, final ArrayList<Path> list) throws IOException { Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (Thread.interrupted()) { throw new IOException(); } if (attrs.isRegularFile()) { list.add(file); } return FileVisitResult.CONTINUE; } }); } }} ---- `JDK 1.8.0`以上の場合は、`Files.walk(Path)`を使用する方法もあります。 - [http://docs.oracle.com/javase/jp/8/api/java/nio/file/Files.html#walk-java.nio.file.Path-java.nio.file.FileVisitOption...- Files#walk(Path) (Java Platform SE 8)] - [http://docs.oracle.com/javase/jp/8/api/docs/java/nio/file/Files.html#walk-java.nio.file.Path-java.nio.file.FileVisitOption...- Files#walk(Path) (Java Platform SE 8)] - [http://www.ne.jp/asahi/hishidama/home/tech/java/files.html#walk Javaファイル関連メモ2(Hishidama's Java Files Memo)] - [http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder java - Read all files in a folder - Stack Overflow] #code{{ import java.util.*; import java.util.stream.*; import java.io.IOException; import java.nio.file.*; public class FilesWalkTest { public static void main(String[] args) { Path dir = Paths.get("."); //Files.walk(dir).forEach(System.out::println); try (Stream<Path> s = Files.walk(dir) .filter(Files::isRegularFile)) { List<Path> l = s.collect(Collectors.toList()); System.out.println(l.size()); } catch (IOException ex) { ex.printStackTrace(); } } } }} * 参考リンク [#oc687cdf] - [http://msugai.fc2web.com/java/IO/fileObj.html Java入門 ファイル] - [http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html How to Use Progress Bars] - [http://docs.oracle.com/javase/jp/6/api/javax/swing/SwingWorker.html SwingWorker (Java Platform SE 6)] - [http://web.archive.org/web/20090830092511/http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html Using a Swing Worker Thread] * コメント [#v7736a30] #comment - 実際に動作するサンプルを追加してみました。 -- &user(aterai); &new{2006-04-28 (金) 21:50:55}; - `JDK 6`の`SwingWorker`を使用するように変更しました。 -- &user(aterai); &new{2008-07-11 (金) 15:32:26}; #comment