23.01_File類遞歸練習(xí)(統(tǒng)計(jì)該文件夾大小)
- 需求:1,從鍵盤接收一個(gè)文件夾路徑,統(tǒng)計(jì)該文件夾大小
/**
* 統(tǒng)計(jì)文件夾大小
*/
public static long getFileLength(File file) {
long length = 0;
for (File f : file.listFiles()) {
if (f.isFile()) {
length += f.length();
}else { //文件夾就遞歸
length += getFileLength(f);
}
}
return length ;
}
/**
* 鍵盤錄入文件夾路徑
* @return 文件夾路徑
*/
public static File getDir() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入文件夾路徑...");
while(true) {
File file = new File(sc.nextLine());
if (!file.exists()) { //是否存在路徑或文件
System.out.println("路徑不存在,請重新輸入...");
continue;
} else if(file.isFile()) { //是文件篓足,就以當(dāng)前文件所在文件夾為目標(biāo)文件夾
sc.close();
return file.getParentFile();
} else {
sc.close();
return file ;
}
}
}
long size = getFileLength(getDir());
float t = (float) (size / 1024.0 / 1024.0 / 1024.0);
DecimalFormat df = new DecimalFormat("#.##"); //四舍五入保留小數(shù)
System.out.println("文件夾大小是: " + df.format(t) + "GB");
System.out.println("文件夾大小是: " + t + "字節(jié)");
23.02_File類遞歸練習(xí)(刪除該文件夾)
- 需求:2,從鍵盤接收一個(gè)文件夾路徑,刪除該文件夾
/**
* 刪除文件夾
* @param file 文件夾路徑
*/
public static void deleteFile(File file) {
for (File f : file.listFiles()) { //如果文件夾不是空的段誊,那么delete()是刪不掉的,
if (f.isFile()) {
f.delete(); //文件就直接刪除
} else {
deleteFile(f); //遞歸刪除文件夾子內(nèi)容
}
}
file.delete();
}
23.03_File類遞歸練習(xí)(拷貝)
- 需求:3,從鍵盤接收兩個(gè)文件夾路徑,把其中一個(gè)文件夾中(包含內(nèi)容)拷貝到另一個(gè)文件夾中
/**
* 將path1文件內(nèi)的子內(nèi)容拷貝到path2文件夾內(nèi)
* @param initPath 源文件夾路徑
* @param path1 源文件夾路徑
* @param path2 目標(biāo)文件夾
* @throws IOException
*/
public static void copyFile(String initPath, File path1, File path2) throws IOException {
if (path1.equals(path2)) return ; //防止相同文件夾造成死循環(huán)
for (File subFile : path1.listFiles()) {
if (subFile.isFile()) { // 是否文件直接拷貝
// 處理路徑問題栈拖,拷貝過去枕扫,要求層級目錄不變
String path = subFile.getPath();
String sp = path.substring(initPath.length()); //得到文件的層級結(jié)構(gòu)
File newDir = new File(path2.getPath() + sp).getParentFile();
if (!newDir.exists()) { //在新文件夾下,建立層級結(jié)構(gòu)
newDir.mkdirs();
}
FileInputStream fis = new FileInputStream(subFile);
FileOutputStream fos = new FileOutputStream(path2.getPath() + sp);
byte[] data = new byte[1024 * 10];
int len ;
while( (len = fis.read(data)) != -1) {
fos.write(data, 0, len);
}
fis.close();
fos.close();
} else { // 文件夾就遞歸
copyFile(initPath, subFile, path2);
}
}
}
23.04_File類遞歸練習(xí)(按層級打印)
- 需求:4,從鍵盤接收一個(gè)文件夾路徑,把文件夾中的所有文件以及文件夾的名字按層級打印
/**
* 按層級結(jié)構(gòu)打印文件夾目錄
* @param file 文件夾路徑
* @param lev 遞歸計(jì)數(shù)器辱魁,調(diào)用時(shí)烟瞧,必須傳入0
*/
public static void printFile(File file , int lev) {
for ( File subFile : file.listFiles() ) {
for(int i = 0 ; i < lev ; i++) { //打印層級結(jié)構(gòu)诗鸭,這種思路很巧妙
System.out.print("\t");
}
System.out.println(subFile); //無論是文件還是文件夾,都需要直接打印
if (subFile.isDirectory()) { //是文件夾就遞歸
printFile(subFile, lev+1); //這里不能 ++ 運(yùn)算符参滴,思考一下為什么
}
}
}
23.05_遞歸練習(xí)(斐波那契數(shù)列)
- 不死神兔
故事得從西元1202年說起强岸,話說有一位意大利青年,名叫斐波那契砾赔。在他的一部著作中提出了一個(gè)有趣的問題:假設(shè)一對剛出生的小兔一個(gè)月后就能長成大兔蝌箍,再過一個(gè)月就能生下一對小兔,并且此后每個(gè)月都生一對小兔暴心,一年內(nèi)沒有發(fā)生死亡妓盲, - 問:一對剛出生的兔子,一年內(nèi)繁殖成多少對兔子?
- 兔子數(shù)量增長數(shù)列: 1 1 2 3 5 8 13...
// 方法二: 遞歸解決
public static int fun(int mou) { // mou為任意月份专普,返回此月份的兔子數(shù)量
if (mou == 1 || mou == 2) {
return 1 ;
}
return fun(mou - 2) + fun(mou - 1) ;
}
// 方法一: 數(shù)組解決
public static void demo1() {
int[] arr = new int[12]; //長度就是多少個(gè)月份
arr[0] = 1; //根據(jù)數(shù)列規(guī)律悯衬,前2個(gè)數(shù)的和就是第三個(gè)的數(shù),而前2個(gè)數(shù)確定是1
arr[1] = 1;
// 遍歷數(shù)組檀夹,對其他月份賦值
for (int i = 2; i < arr.length; i++) {
arr[i] = arr[i-1] + arr[i-2];
}
System.out.println(arr[12]);
}
23.06_遞歸練習(xí)(1000的階乘所有零和尾部零的個(gè)數(shù))
- 需求:求出1000的階乘所有零和尾部零的個(gè)數(shù),不用遞歸做
BigInteger bi1 = new BigInteger("1");
for(int i = 1 ; i <= 1000 ; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2); //將 bi1 和 bi2的結(jié)果相乘再賦值給bi1
}
String str = bi1.toString();
int count = 0 ;
for (char c : str.toCharArray()) {
if ('0' == c) count++ ;
}
System.out.println("所有零的個(gè)數(shù):" + count);
// 字符串反轉(zhuǎn) (鏈?zhǔn)骄幊?筋粗,最好記住下面的方法
str = new StringBuilder(str).reverse().toString();
int num= 0 ;
for(char c : str.toCharArray()) {
if('0' != c) break ;
num++;
}
System.out.println("尾部零的個(gè)數(shù):" + num);
23.07_遞歸練習(xí)(1000的階乘尾部零的個(gè)數(shù))
- 需求:求出1000的階乘尾部零的個(gè)數(shù),用遞歸做
public static int fun(int num) {
if (num > 0 && num < 5) {
return 0;
} else {
return num / 5 + fun(num / 5);
}
}
// 推導(dǎo)過程十分麻煩,百度吧炸渡,比如下面鏈接的講解
http://blog.csdn.net/yahohi/article/details/7528803
23.08_集合練習(xí)(約瑟夫環(huán))
- 幸運(yùn)數(shù)字
/**
* 約瑟夫環(huán)娜亿,幸運(yùn)數(shù)字
* @param num 人數(shù),多少人進(jìn)約瑟夫環(huán)游戲
* @return 最后那個(gè)幸運(yùn)的人的位置
*/
public static int getLuckNum(int num) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= num; i++) { //將1...num個(gè)元素存儲到集合里
list.add(i);
}
int count = 1 ; //用來數(shù)數(shù)蚌堵,計(jì)算器买决,只要是3的倍數(shù)就殺人
for(int i = 0; list.size() != 1 ; i++) { //只要集合人數(shù)不是1,就循環(huán)殺人
if (i == list.size()) i = 0 ; //到數(shù)組末尾吼畏,就重新從頭開始遍歷
if (count % 3 == 0) list.remove(i--); //如果是3的倍數(shù)策州,就殺人。,--是因?yàn)閿?shù)組元素減少了宫仗,索引也要跟著減少
count++;
}
return list.get(0);
}
END够挂。
我是小侯爺。
在魔都艱苦奮斗藕夫,白天是上班族孽糖,晚上是知識服務(wù)工作者。
如果讀完覺得有收獲的話毅贮,記得關(guān)注和點(diǎn)贊哦办悟。
非要打賞的話,我也是不會拒絕的滩褥。