Personal tools
You are here: Home documents lectures Java Sample Programs フォルダ内の全ファイルをリストにして取得する
Document Actions

フォルダ内の全ファイルをリストにして取得する

by member last modified 2007-03-21 19:26

指定されたフォルダ内にある、特定の拡張子がついたファイルだけをすべて集めて、Vectorとして返します。フォルダが入れ子になっている場合は中もすべて見ます。

   import java.util.Stack;
import java.util.Vector;

static public Vector getFilenames(String file,
String extension)
{
Vector V = new Vector();
Stack S = new Stack();
S.push(new File(file));
try
{
while (!S.empty())
{
File f = S.pop();
if (f.isDirectory())
{
File[] children = f.listFiles();
for (int i = 0; i < children.length; i++)
S.push(children[i]);
}
else
{
if (f.getName().endsWith(".mol"))
V.add(f);
}
}
}
catch (Exception e)
{
System.out.println(e);
}
return V;
}

Powered by Plone, the Open Source Content Management System

This site conforms to the following standards: