1、使用递归将字符串进行反转
package edu.uestc.avatar;
public class Recursion {
public static void main(String[] args) {
String str = "abcdefg";
System.out.println(StrReverse(str));
}
public static String StrReverse(String str) {
if(str.length()<=1||str==null) {
return str;
}
return StrReverse(str.substring(1)) + str.charAt(0);
}
}
2、计算指定目录下所有文件大小总和
package edu.uestc.avatar;
import java.io.File;
public class FileTotalSize {
private static long length;
public static void main(String[] args) {
// TODO Auto-generated method stub
File file =new File("X:\\XXX");//指定文件夹
TotalSize(file);
System.out.println(length);
}
public static void TotalSize(File file) {
if(file.isFile()) {
length +=file.length();
}
else {
File[] files = file.listFiles();
for(int i = 0; files != null && i < files.length; i++) {
TotalSize(files[i]);
}
}
}
}