1、递归求5的阶乘

  1. package com.heima.chario;
  2. public class Demo8_Digui {
  3. /**
  4. * @param args
  5. * 递归:方法自己调用自己
  6. * 5!
  7. * 5 * 4 * 3 * 2 * 1
  8. *
  9. * 5 * fun(4)(代表4!)
  10. * 4 * fun(3)(代表3!)
  11. * 3 * fun(2)(代表2!)
  12. * 2 * fun(1)(代表1!)
  13. * 递归的弊端:不能调用次数过多,容易导致栈内存溢出
  14. * 递归的好处:不用知道循环次数
  15. *
  16. * 构造方法是否可以递归调用?
  17. * 构造方法不能使用递归调用
  18. *
  19. * 递归调用是否必须有返回值?
  20. * 不一定(可以有,也可以没有)
  21. */
  22. public static void main(String[] args) {
  23. /*int result = 1;
  24. for(int i = 1; i <= 5; i++) {
  25. result = result * i;
  26. }
  27. System.out.println(result);*/
  28. System.out.println(fun(6000));
  29. }
  30. public static int fun(int num) {
  31. if(num == 1) {
  32. return 1;
  33. }else {
  34. return num * fun(num - 1);
  35. }
  36. }
  37. }


2、从键盘接收一个文件夹路径,统计该文件夹大小

  1. package com.heima.test;
  2. import java.io.File;
  3. import java.util.Scanner;
  4. public class Test1 {
  5. /**
  6. * @param args
  7. * 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
  8. *
  9. * 从键盘接收一个文件夹路径
  10. * 1,创建键盘录入对象
  11. * 2,定义一个无限循环
  12. * 3,将键盘录入的结果存储并封装成File对象
  13. * 4,对File对象判断
  14. * 5,将文件夹路径对象返回
  15. *
  16. * 统计该文件夹大小
  17. * 1,定义一个求和变量
  18. * 2,获取该文件夹下所有的文件和文件夹listFiles();
  19. * 3,遍历数组
  20. * 4,判断是文件就计算大小并累加
  21. * 5,判断是文件夹,递归调用
  22. */
  23. public static void main(String[] args) {
  24. //File dir = new File("F:\\day06");
  25. //System.out.println(dir.length()); //直接获取文件夹的结果是0
  26. File dir = getDir();
  27. System.out.println(getFileLength(dir));
  28. }
  29. /*
  30. * 从键盘接收一个文件夹路径
  31. * 1,返回值类型File
  32. * 2,参数列表无
  33. */
  34. public static File getDir() {
  35. //1,创建键盘录入对象
  36. Scanner sc = new Scanner(System.in);
  37. System.out.println("请输入一个文件夹路径:");
  38. //2,定义一个无限循环
  39. while(true) {
  40. //3,将键盘录入的结果存储并封装成File对象
  41. String line = sc.nextLine();
  42. File dir = new File(line);
  43. //4,对File对象判断
  44. if(!dir.exists()) {
  45. System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:");
  46. }else if(dir.isFile()) {
  47. System.out.println("您录入的是文件路径,请输入一个文件夹路径:");
  48. }else {
  49. //5,将文件夹路径对象返回
  50. return dir;
  51. }
  52. }
  53. }
  54. /*
  55. * 统计该文件夹大小
  56. * 1,返回值类型long
  57. * 2,参数列表File dir
  58. */
  59. public static long getFileLength(File dir) { //dir = F:\day06\day07
  60. //1,定义一个求和变量
  61. long len = 0;
  62. //2,获取该文件夹下所有的文件和文件夹listFiles();
  63. File[] subFiles = dir.listFiles(); //day07 Demo1_Student.class Demo1_Student.java
  64. //3,遍历数组
  65. for (File subFile : subFiles) {
  66. //4,判断是文件就计算大小并累加
  67. if(subFile.isFile()) {
  68. len = len + subFile.length();
  69. //5,判断是文件夹,递归调用
  70. }else {
  71. len = len + getFileLength(subFile);
  72. }
  73. }
  74. return len;
  75. }
  76. }


3、从键盘接收一个文件夹路径,删除该文件夹

  1. package com.heima.test;
  2. import java.io.File;
  3. public class Test2 {
  4. /**
  5. * 需求:2,从键盘接收一个文件夹路径,删除该文件夹
  6. *
  7. * 删除该文件夹
  8. * 分析:
  9. * 1,获取该文件夹下的所有的文件和文件夹
  10. * 2,遍历数组
  11. * 3,判断是文件直接删除
  12. * 4,如果是文件夹,递归调用
  13. * 5,循环结束后,把空文件夹删掉
  14. */
  15. public static void main(String[] args) {
  16. File dir = Test1.getDir(); //获取文件夹路径
  17. deleteFile(dir);
  18. }
  19. /*
  20. * 删除该文件夹
  21. * 1,返回值类型 void
  22. * 2,参数列表File dir
  23. */
  24. public static void deleteFile(File dir) {
  25. //1,获取该文件夹下的所有的文件和文件夹
  26. File[] subFiles = dir.listFiles();
  27. //2,遍历数组
  28. for (File subFile : subFiles) {
  29. //3,判断是文件直接删除
  30. if(subFile.isFile()) {
  31. subFile.delete();
  32. //4,如果是文件夹,递归调用
  33. }else {
  34. deleteFile(subFile);
  35. }
  36. }
  37. //5,循环结束后,把空文件夹删掉
  38. dir.delete();
  39. }
  40. }

4、从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

  1. package com.heima.test;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. public class Test3 {
  10. /**
  11. * 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  12. *
  13. * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  14. * 分析:
  15. * 1,在目标文件夹中创建原文件夹
  16. * 2,获取原文件夹中所有的文件和文件夹,存储在File数组中
  17. * 3,遍历数组
  18. * 4,如果是文件就用io流读写
  19. * 5,如果是文件夹就递归调用
  20. * @throws IOException
  21. */
  22. public static void main(String[] args) throws IOException {
  23. File src = Test1.getDir();
  24. File dest = Test1.getDir();
  25. if(src.equals(dest)) {
  26. System.out.println("目标文件夹是源文件夹的子文件夹");
  27. }else {
  28. copy(src,dest);
  29. }
  30. }
  31. /*
  32. * 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
  33. * 1,返回值类型void
  34. * 2,参数列表File src,File dest
  35. */
  36. public static void copy(File src, File dest) throws IOException {
  37. //1,在目标文件夹中创建原文件夹
  38. File newDir = new File(dest, src.getName());
  39. newDir.mkdir();
  40. //2,获取原文件夹中所有的文件和文件夹,存储在File数组中
  41. File[] subFiles = src.listFiles();
  42. //3,遍历数组
  43. for (File subFile : subFiles) {
  44. //4,如果是文件就用io流读写
  45. if(subFile.isFile()) {
  46. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
  47. BufferedOutputStream bos =
  48. new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
  49. int b;
  50. while((b = bis.read()) != -1) {
  51. bos.write(b);
  52. }
  53. bis.close();
  54. bos.close();
  55. //5,如果是文件夹就递归调用
  56. }else {
  57. copy(subFile,newDir);
  58. }
  59. }
  60. }
  61. }

5、从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印

  1. package com.heima.test;
  2. import java.io.File;
  3. public class Test4 {
  4. /**
  5. * 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
  6. * 把文件夹中的所有文件以及文件夹的名字按层级打印
  7. * 分析:
  8. * 1,获取所有文件和文件夹,返回的File数组
  9. * 2,遍历数组
  10. * 3,无论是文件还是文件夹,都需要直接打印
  11. * 4,如果是文件夹,递归调用
  12. * day07
  13. * day08
  14. * xxx.jpg
  15. * yyy.txt
  16. * Demo1_Consturctor.class
  17. * Demo1_Consturctor.java
  18. * Demo1_Student.class
  19. * Demo1_Student.java
  20. */
  21. public static void main(String[] args) {
  22. File dir = Test1.getDir(); //获取文件夹路径
  23. printLev(dir,0);
  24. }
  25. public static void printLev(File dir,int lev) {
  26. //1,把文件夹中的所有文件以及文件夹的名字按层级打印
  27. File[] subFiles = dir.listFiles();
  28. //2,遍历数组
  29. for (File subFile : subFiles) {
  30. for(int i = 0; i <= lev; i++) {
  31. System.out.print("\t");
  32. }
  33. //3,无论是文件还是文件夹,都需要直接打印
  34. System.out.println(subFile);
  35. //4,如果是文件夹,递归调用
  36. if(subFile.isDirectory()) {
  37. //printLev(subFile,lev + 1);
  38. printLev(subFile,++lev);
  39. }
  40. }
  41. }
  42. }

方法2
  1. import java.io.File;
  2. /**
  3. * 打印层级目录
  4. * @author haoyongliang
  5. *
  6. */
  7. public class Demo5 {
  8. public static void main(String[] args) {
  9. show("E:\\JackDB-SQL-Database-Client_v3.0.0",0);
  10. }
  11. public static void show(String fileDir, int level){
  12. File file = new File(fileDir);
  13. //1、打印空格
  14. for(int i=0; i<level; i++){
  15. System.out.print("\t");
  16. }
  17. //2、打印文件或者文件夹名
  18. System.out.println(file.getAbsolutePath());
  19. //3、如果要是文件夹,继续递归
  20. if(file.isDirectory()){
  21. File[] listFiles = file.listFiles();
  22. if(listFiles != null){
  23. for(File f : listFiles){
  24. show(f.getAbsolutePath(),level+1);
  25. }
  26. }
  27. }
  28. }
  29. }


6、斐波那契-不死神兔

  1. package com.heima.test;
  2. public class Test5 {
  3. /**
  4. * * 不死神兔
  5. * 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
  6. * 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
  7. * 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
  8. * 1 1 2 3 5 8 13 21
  9. * 1 = fun(1)
  10. * 1 = fun(2)
  11. * 2 = fun(1) + fun(2)
  12. * 3 = fun(2) + fun(3)
  13. */
  14. public static void main(String[] args) {
  15. //demo1();
  16. System.out.println(fun(8));
  17. }
  18. public static void demo1() {
  19. //用数组做不死神兔
  20. int[] arr = new int[8];
  21. //数组中第一个元素和第二个元素都为1
  22. arr[0] = 1;
  23. arr[1] = 1;
  24. //遍历数组对其他元素赋值
  25. for(int i = 2; i < arr.length; i++) {
  26. arr[i] = arr[i - 2] + arr[i - 1];
  27. }
  28. //如何获取最后一个数
  29. System.out.println(arr[arr.length - 1]);
  30. }
  31. /*
  32. * 用递归求斐波那契数列
  33. */
  34. public static int fun(int num) {
  35. if(num == 1 || num == 2) {
  36. return 1;
  37. }else {
  38. return fun(num - 2) + fun(num - 1);
  39. }
  40. }
  41. }



7、求出1000的阶乘所有零和尾部零的个数,不用递归做

  1. package com.heima.test;
  2. import java.math.BigInteger;
  3. public class Test6 {
  4. /**
  5. * @param args
  6. * 需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
  7. */
  8. public static void main(String[] args) {
  9. /*int result = 1;
  10. for(int i = 1; i <= 1000; i++) {
  11. result = result * i;
  12. }
  13. System.out.println(result); //因为1000的阶乘远远超出了int的取值范围
  14. */
  15. //demo1();
  16. demo2();
  17. }
  18. public static void demo2() { //获取1000的阶乘尾部有多少个零
  19. BigInteger bi1 = new BigInteger("1");
  20. for(int i = 1; i <= 1000; i++) {
  21. BigInteger bi2 = new BigInteger(i+"");
  22. bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1
  23. }
  24. String str = bi1.toString(); //获取字符串表现形式
  25. StringBuilder sb = new StringBuilder(str);
  26. str = sb.reverse().toString(); //链式编程
  27. int count = 0; //定义计数器
  28. for(int i = 0; i < str.length(); i++) {
  29. if('0' != str.charAt(i)) {
  30. break;
  31. }else {
  32. count++;
  33. }
  34. }
  35. System.out.println(count);
  36. }
  37. public static void demo1() { //求1000的阶乘中所有的零
  38. BigInteger bi1 = new BigInteger("1");
  39. for(int i = 1; i <= 1000; i++) {
  40. BigInteger bi2 = new BigInteger(i+"");
  41. bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1
  42. }
  43. String str = bi1.toString(); //获取字符串表现形式
  44. int count = 0;
  45. for(int i = 0; i < str.length(); i++) {
  46. if('0' == str.charAt(i)) { //如果字符串中出现了0字符
  47. count++; //计数器加1
  48. }
  49. }
  50. System.out.println(count);
  51. }
  52. }


8、约瑟夫环

  1. package com.heima.test;
  2. import java.util.ArrayList;
  3. public class Test8 {
  4. /**
  5. * @param args
  6. * 约瑟夫环
  7. * * 幸运数字
  8. */
  9. public static void main(String[] args) {
  10. System.out.println(getLucklyNum(8));
  11. }
  12. /*
  13. * 获取幸运数字
  14. * 1,返回值类型int
  15. * 2,参数列表int num
  16. */
  17. public static int getLucklyNum(int num) {
  18. ArrayList<Integer> list = new ArrayList<>(); //创建集合存储1到num的对象
  19. for(int i = 1; i <= num; i++) {
  20. list.add(i); //将1到num存储在集合中
  21. }
  22. int count = 1; //用来数数的,只要是3的倍数就杀人
  23. for(int i = 0; list.size() != 1; i++) { //只要集合中人数超过1,就要不断的杀
  24. if(i == list.size()) { //如果i增长到集合最大的索引+1时
  25. i = 0; //重新归零
  26. }
  27. if(count % 3 == 0) { //如果是3的倍数
  28. list.remove(i--); //就杀人
  29. }
  30. count++;
  31. }
  32. return list.get(0);
  33. }
  34. }

9、今天必须掌握的内容,面试题,笔试题。(掌握这个就可以放心学习后面的知识了)

1、所有代码必须掌握
2、编写一个程序,把指定目录下的所有文件拷贝到另一个目录中, 拷贝成功后,把后缀名是.java的改成.txt


Day23_IO第五天的更多相关文章

  1. 《Django By Example》第五章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者@ucag注:大家好,我是新来的翻译, ...

  2. 旺财速啃H5框架之Bootstrap(五)

    在上一篇<<旺财速啃H5框架之Bootstrap(四)>>做了基本的框架,<<旺财速啃H5框架之Bootstrap(二)>>篇里也大体认识了bootst ...

  3. 移动站应该尝试百度MIP的五个原因

    MIP是什么?MIP是百度在2016年提出的移动网页加速器项目. MIP能做什么?MIP能帮助站长和网站开发者快速搭建移动端页面. MIP怎么加速?MIP从前端渲染和页面网络传输两方面进行优化,杜绝页 ...

  4. 如何一步一步用DDD设计一个电商网站(五)—— 停下脚步,重新出发

    阅读目录 前言 单元测试 纠正错误,重新出发 结语 一.前言 实际编码已经写了2篇了,在这过程中非常感谢有听到观点不同的声音,借着这个契机,今天这篇就把大家提出的建议一个个的过一遍,重新整理,重新出发 ...

  5. MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信

    MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...

  6. 【原】AFNetworking源码阅读(五)

    [原]AFNetworking源码阅读(五) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中提及到了Multipart Request的构建方法- [AFHTTP ...

  7. JavaScript常见的五种数组去重的方式

    ▓▓▓▓▓▓ 大致介绍 JavaScript的数组去重问题在许多面试中都会遇到,现在做个总结 先来建立一个数组 var arr = [1,2,3,3,2,'我','我',34,'我的',NaN,NaN ...

  8. 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载

    title: 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载 tags: -RecyclerView,下拉刷新,上拉加载更多 grammar_cjkRuby: true - ...

  9. CRL快速开发框架系列教程五(使用缓存)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

随机推荐

  1. Understanding the Internal Message Buffers of Storm

    Understanding the Internal Message Buffers of Storm Jun 21st, 2013 Table of Contents Internal messag ...

  2. 深入了解 JavaScript 中的 for 循环

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...

  3. python 获取脚本所在目录

    平时写python经常会想获得脚本所在的目录,例如有个文件跟脚本文件放在一个相对的目录位置,那就可以通过脚本文件的目录找到对应的文件,即使以后脚本文件移到其他地方,脚本也基本不需要改动(相对于写死目录 ...

  4. python学习05——字典

    笨办法学python第39节 这节主要讲解的是字典,首先字典和列表的两个区别是: 1. 列表中可以通过数字找到列表中的元素,是数字作为索引的:字典中可以通过任何东西找到想要的元素,即字典可以将一个物件 ...

  5. Man——send(2)翻译

    ##纯手打 Man——send(2) -->NAME: send, sendto, sendmsg - 在socket上发送一条消息 -->总览: #include <sys/typ ...

  6. textbox只能输入数字或中文的常用正则表达式和验证方法

    验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...

  7. 封装pyMysql

    #!/usr/bin/python import MySQLdb class SpiderPDO: def __init__(self): db_host = '127.0.0.1' db_user ...

  8. android 解析XML 工具类

    /** * Created by John on 2016/3/29. */ public class XmlParser { private static final String ns = nul ...

  9. 2.4G/5G频段WLAN各国使用信道表

    List of WLAN channels (维基百科):https://en.wikipedia.org/wiki/List_of_WLAN_channels 2.4G 5G 另附美国5G允许使用的 ...

  10. sac 文档使用

    目前我遇到的问题是我想要得到BHE,BHN 方向的数据,但发现IRIS下载的数据都是BH1,BH2 方向的,很困惑,请教大神后发现,原来IRIS之所以提供BH1,BH2方向是因为很多时候台站的水平方向 ...