package com.test; import java.io.File; import java.util.Scanner; public class Test1 { /** * 需求:从键盘接受一个文件夹路径,统计该文件夹大小 * * 从键盘接受一个文件夹路径 * 1、创建键盘录入对象 * 2、定义一个无限循环 * 3、将键盘录入的结果存储并封装成File对象 * 4、对File对象判断 * 5、将文件夹路径对象返回 * * 统计该文件夹大小 * 1、定义一个求和变量 * 2、获取该文件夹下所有的文件和文件夹listFiles() * 3、遍历数组 * 4、判断是文件就计算大小并累加 * 5、判断是文件夹,递归调用 */ public static void main(String[] args) { //File dir = new File("E:\\javaStudy\\2019-06-01"); //直接获取文件夹的结果为0 //System.out.println(dir.length()); File dir = getDir(); System.out.println(getFileLength(dir)); } /** * 从键盘接收一个文件夹路径 * 1、返回值类型File * 2、参数列表无 */ public static File getDir(){ //1、创建键盘录入对象 Scanner sc =new Scanner(System.in); System.out.println("请输入一个文件夹路径:"); //2、定义一个无限循环 while(true) { //3、将键盘录入的结果存储并封装成File对象 String line = sc.nextLine(); File dir = new File(line); //4、对File对象判断 if (!dir.exists()) { System.out.println("您输入的文件夹路径不存在,请重新输入一个文件夹路径:"); }else if (dir.isFile()) { System.out.println("您输入的是文件路径不存在,请重新输入一个文件夹路径:"); }else { //5、将文件夹路径对象返回 return dir; } } } /** * 统计该文件夹大小 * 1、返回值类型long * 2、参数列表File dir */ public static long getFileLength(File dir){ //1、定义一个求和变量 long len = 0; //2、获取该文件夹下所有的文件和文件夹listFiles() File[] subFiles = dir.listFiles(); //3、遍历数组 for (File subFile : subFiles) { //4、判断是文件就计算大小并累加 if (subFile.isFile()) { len += subFile.length(); //5、判断是文件夹,递归调用 }else{ len += getFileLength(subFile); } } return len; } }
package com.test; import java.io.File; public class Test2 { /** * 需求:从键盘接收一个文件夹路径,删除该文件 * * 删除该文件夹 * 分析: * 1、获取该文件夹下的所有文件和文件夹 * 2、遍历数组 * 3、判断是文件直接删除 * 4、如果是文件夹,递归调用 * 5、循环结束后,把空文件夹删掉 */ public static void main(String[] args) { File dir = Test1.getDir(); //获取文件夹路径 deleteFile(dir); } /** * 删除该文件夹 * 1、返回值类型void * 2、参数列表File dir */ public static void deleteFile(File dir){ //1、获取该文件夹下的所有文件和文件夹 File[] subFiles = dir.listFiles(); //2、遍历数组 for (File subFile : subFiles) { //3、判断是文件直接删除 if (subFile.isFile()) { subFile.delete(); }else{ deleteFile(subFile); } } //5、循环结束后,把空文件夹删掉 dir.delete(); } }
package com.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Test3 { /** * 需求:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中 * * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中 * 分析: * 1、在目标文件夹中创建原文件夹 * 2、获取原文件夹中所有的文件和文件夹,存储在File数组中 * 3、遍历数组 * 4、如果是文件就用io流读写 * 5、如果是文件夹就递归调用 * @throws IOException */ public static void main(String[] args) throws IOException { File src = Test1.getDir(); File dest = Test1.getDir(); if (src.equals(dest)) { System.out.println("目标文件夹是原文件夹的子文件夹"); }else { copy(src,dest); } } /** * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中 * 1、返回值类型void * 2、参数列表File src,File dest * @throws IOException */ private static void copy(File src, File dest) throws IOException { //1、在目标文件夹中创建原文件夹 File newDir = new File(dest,src.getName()); newDir.mkdir(); //2、获取原文件夹中所有的文件和文件夹,存储在File数组中 File[] subFiles = src.listFiles(); //3、遍历数组 for (File subFile : subFiles) { //4、如果是文件就用io流读写 if (subFile.isFile()) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName()))); int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); bos.close(); //5、如果是文件夹就递归调用 }else { copy(subFile, newDir); } } } }
package com.test; import java.io.File; public class Test4 { /** *需求:从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印 * * 把文件夹中的所有文件以及文件夹的名字按层级打印 * 分析: * 1、获取所有文件和文件夹,返回的File数组 * 2、遍历数组 * 3、无论是文件还是文件夹,都需要直接打印 * 4、如果是文件夹,递归调用 */ public static void main(String[] args) { File dir = Test1.getDir(); //获取文件夹路径 printLev(dir,0); } private static void printLev(File dir, int lev) { //1、获取所有文件和文件夹,返回的File数组 File[] subFiles = dir.listFiles(); //2、遍历数组 for (File subFile : subFiles) { for (int i = 0; i < lev; i++) { System.out.print("\t"); } //3、无论是文件还是文件夹,都需要直接打印 System.out.println(subFile); //4、如果是文件夹,递归调用 if (subFile.isDirectory()) { //printLev(subFile, lev + 1); printLev(subFile, ++lev); } } } }
package com.test; public class Test5 { /** * * 不死神兔 * 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。 * 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡, * 问:一对刚出生的兔子,一年内繁殖成多少对兔子? * 1 1 2 3 5 8 13 21 * 1 = fun(1) * 1 = fun(2) * 2 = fun(1) + fun(2) * 3 = fun(2) + fun(3) */ public static void main(String[] args) { //demo1(); System.out.println(fun(8)); } public static void demo1() { //用数组最不死神兔 int[] arr = new int[8]; //数组中第一个元素和第二个元素都为1 arr[0] = 1; arr[1] = 1; //遍历数组对其他元素赋值 for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 2] + arr[i - 1]; } //如何获取最后一个数 System.out.println(arr[arr.length - 1]); } public static int fun(int num){ if (num == 1 || num == 2) { return 1; }else { return fun(num - 2) + fun(num - 1); } } }
package com.test; import java.math.BigInteger; public class Test6 { /** * 需求:求出1000的继承所有零和尾部零的个数,不用递归做 */ public static void main(String[] args) { /*int result = 1; for (int i = 0; i < 1000; i++) { result = result * i; } System.out.println(result);*/ //因为1000的阶乘远远超出了int的取值范围 //demo1(); demo2(); } public static void demo2() { //获取1000的阶乘尾部有多少个零 BigInteger bi1 = new BigInteger("1"); for (int i = 1; i <= 1000; i++) { BigInteger bi2 = new BigInteger(i+""); bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1 } String str = bi1.toString(); //获取字符串表现形式 StringBuilder sb = new StringBuilder(str); str = sb.reverse().toString(); //链式编程 int count = 0; //定义计数器 for (int i = 0; i < str.length(); i++) { if (‘0‘ != str.charAt(i)) { //如果字符串中出现了0字符 break; }else { count++; } } System.out.println(count); } public static void demo1() { //求1000的阶乘中所有的零 BigInteger bi1 = new BigInteger("1"); for (int i = 1; i <= 1000; i++) { BigInteger bi2 = new BigInteger(i+""); bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1 } String str = bi1.toString(); //获取字符串表现形式 int count = 0; for (int i = 0; i < str.length(); i++) { if (‘0‘ == str.charAt(i)) { //如果字符串中出现了0字符 count++; } } System.out.println(count); } }
package com.test; public class Test7 { /** * 需求:求出1000的阶乘尾部零的个数,用递归做 */ public static void main(String[] args) { System.out.println(fun(1000)); } public static int fun(int num){ if (num > 0 && num < 5) { return 0; }else{ return num / 5 + fun(num / 5); } } }
package com.test; import java.util.ArrayList; public class Test8 { /** * 约瑟夫环 * 幸运数字 */ public static void main(String[] args) { System.out.println(getLucklyNum(8)); } private static int getLucklyNum(int num) { ArrayList<Integer> list = new ArrayList<>(); //创建集合存储1到num的对象 for (int i = 1; i <= num; i++) { list.add(i); //将1到num存储在集合中 } int count = 1; //用来数数的,只要是3的倍数就杀人 for (int i = 0; list.size() != 1; i++) { //只要集合中人数超过1,就要不断的啥 if (i == list.size()) { //如果i增长到集合最大的索引+1时 i = 0; } if (count % 3 == 0) { //如果是3的倍数 list.remove(i--); //就杀人 } count++; } return list.get(0); } }
原文:https://www.cnblogs.com/clqbolg/p/10972962.html