java在已有文件夹中创建文件,要用递归的方法
1个回答
展开全部
类似的,提问者只要在里面new File就够了
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class T1
{
static int k = 0;
static String[] puts = new String[0];
public static void main ( String[] args )
{
File file = new File ("I:/WinRAR");
List<String> list = new ArrayList<String> ();
listFiles (file, list);
System.out.println (list);
System.out.println (list.size ());
System.out.println (Arrays.toString (puts));
System.out.println (puts.length);
}
private static void listFiles ( File file, List<String> list )
{
if (file.isDirectory ())
{
File[] files = file.listFiles ();
for ( File f : files )
{
if (f.isDirectory ())
{
listFiles (f, list);
}
else
{
if (puts.length == 0)
{
puts = new String[1];
}
else
{
String[] temp = new String[k + 1];
System.arraycopy (puts, 0, temp, 0, puts.length);
puts = temp;
}
puts[k] = f.getPath ();
k++;
list.add (f.getAbsolutePath ());
}
}
}
else
{
if (puts.length == 0)
{
puts = new String[1];
}
else
{
String[] temp = new String[k + 1];
System.arraycopy (puts, 0, temp, 0, puts.length);
puts = temp;
}
puts[k] = file.getPath ();
k++;
list.add (file.getAbsolutePath ());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询