列出指定目录下文件或者文件夹,包含子目录中的内容。也就是列出指定目录下所有内容。因为目录中还有目录,只要使用同一个列出目录功能的函数完成即可。在列出过程中出现的还是目录的话,还可以再次调本功能。也就是函数自身调用自身。这种表现形式,或者编程手法,成为递归。
递归要注意:
1.限定条件
2.要注意递归的次数,尽量避免内存溢出。
import java.io.File;
public class FileDemo1 {
public static void main(String[] args) {
File dir = new File("/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src");
showDir(dir);
}
public static void showDir(File dir)
{
System.out.println(dir);
File[] files = dir.listFiles();
for (int x = 0;x<files.length;x++)
{
if (files[x].isDirectory())
showDir(files[x]);
else
System.out.println(files[x]);
}
}
}
//输出
所有文件
import java.io.File;
public class FileDemo1 {
public static void main(String[] args) {
toBin(6);
}
public static void toBin(int num)
{
if (num > 0)
{
toBin(num/2);
System.out.println(num%2);
}
}
}
//输出
1
1
0
import java.io.File;
public class FileDemo1 {
public static void main(String[] args) {
int n = getSum(8000);
System.out.println("n=" + n);
}
public static int getSum(int n) {
if (n == 1)
return 1;
return n + getSum(n - 1);
}
}
//输出
n=32004000
递归次数太大,内存溢出。
import java.io.File;
public class FileDemo1 {
public static void main(String[] args) {
int n = getSum(80000);
System.out.println("n=" + n);
}
public static int getSum(int n) {
if (n == 1)
return 1;
return n + getSum(n - 1);
}
}
//输出
Exception in thread "main" java.lang.StackOverflowError
at FileDemo1.getSum(FileDemo1.java:15)
at FileDemo1.getSum(FileDemo1.java:15)
at FileDemo1.getSum(FileDemo1.java:15)
at FileDemo1.getSum(FileDemo1.java:15)
at FileDemo1.getSum(FileDemo1.java:15)
at FileDemo1.getSum(FileDemo1.java:15)
列出目录下面所有内容,带层次。
import java.io.File;
public class FileDemo2 {
public static void main(String[] args) {
File dir = new File("/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src");
showDir(dir,0);
}
public static String getLevel(int level)
{
StringBuilder sb = new StringBuilder();
sb.append("|--");
for (int x=0;x<level;x++)
{
//sb.append(|--);
sb.insert(0,"| ");
}
return sb.toString();
}
public static void showDir(File dir,int level) {
System.out.println(getLevel(level)+dir.getName());
level++;
System.out.println(dir);
File[] files = dir.listFiles();
for (int x = 0; x < files.length; x++) {
if (files[x].isDirectory())
showDir(files[x],level);
else
System.out.println(getLevel(level)+files[x]);
}
}
}
//输出
|--src
/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/LinkedListTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MathDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo4.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MyBufferedReaderDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileWriterDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionToArrayDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/HashSetDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo5.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CalendarDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/DemoA.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ParamMethodDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo8.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Consumer.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ListDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayListTest0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo9.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/RandomDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayListTest1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo8.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo2.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ListDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FuShuException0.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/.DS_Store
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArraysDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionsDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InputOutputDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MyLineNumberReaderDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo4.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/IntegerDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/LineNumberReaderDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/HelloWorld.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileWriterDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo12.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ReadIn.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/SystemInfo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo5.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FuShuException.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CopyPic.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/DemoB.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo0.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Producer.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/RuntimeDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PersonDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/LinkedListDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayToolDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionInfo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Teacher.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ObjectDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionsDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayTool.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileReaderDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo6.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ProducerConsumerDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ProducerConsumerDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExtendsDemo.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/DateDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PriorityYeildDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo1.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Zi.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Resource.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionsDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo3.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ListDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Fu.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/BankDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionsDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExtendsDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/VectorDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Demo2.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CopyText.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Demo0.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/JoinDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/BufferedReaderDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PackageDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StaticImportDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/LinkedListDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileStream.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo4.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Person.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/HashSetDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/EmployeeDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InputOutputDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/BufferedWriterDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PolymorphicDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/SystemDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CopyMp3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/HelloWorld.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/HashSetDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/NoPlanException.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo5.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ProducerConsumerDemo.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StringDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CalendarDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Polymorphic.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StopThreadDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ProducerConsumerDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/InnerClassDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MyBufferedReaderDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileReaderTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CollectionsDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StopThreadDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo7.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/DeadLockDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo11.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayListTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Demo1.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo4.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileStream.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo6.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileWriterDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/CopyTextByBuf.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ArrayListTest2.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Computer.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MyLineNumberReaderDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/Demo3.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PolymorphicDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/DemoB.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MaoYanException.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StringTest.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ThreadDemo1.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TransStreamDemo4.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ForEachDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/PackageDemo.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/LanPingException.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/TreeSetDemo5.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/SystemDemo.class
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/ExceptionDemo7.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/MapDemo3.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/FileReaderDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo10.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/StopThreadDemo0.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/AbstractDemo.java
| |--/Users/Cloud/Documents/Java/WorkSpace/HelloWorld/src/GenericDemo6.javaCloud
网友评论