A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 695065397 初级黑马   /  2019-5-7 14:59  /  750 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 小石姐姐 于 2019-5-9 18:33 编辑

  • File类的概述和构造方法

    • 概念:文件和目录的抽象表示
    • 构造方法

      • new File("完整路径");
      • new File("目录",“文件”);
      • File file1=new File("目录") File file2=new File(file1,"文件");


  • File类创建类方法

    • 创建文件:createNewFile();
    • 目录:mkdir() ;
    • 多级目录:mkdirs()

  • File类的判断和获取方法

    • 判断是否是目录:isDirectory()
    • 判断是否是文件:isFile()
    • 判断文件或目录是否存在:exists()
    • 名字:getName()
    • 路径:getPath()
    • 全路径:getAbsolutePath()
    • 获取目录下文件名:list()
    • 获取目录下文件:listFiles()

  • File类删除

    • ​        删除文件:delete()删除目录时,目录必须为空

  • 不死神兔例子
  • public class DiGuiDemo {    public static void main(String[] args) {        //回顾不死神兔问题,求第20个月兔子的对数        //每个月的兔子对数:1,1,2,3,5,8,...        int[] arr = new int[20];        arr[0] = 1;        arr[1] = 1;        for (int i = 2; i < arr.length; i++) {            arr = arr[i - 1] + arr[i - 2];        }        System.out.println(arr[19]);        System.out.println(f(20));    }    /*        递归解决问题,首先就是要定义一个方法:            定义一个方法f(n):表示第n个月的兔子对数            那么,第n-1个月的兔子对数该如何表示呢?f(n-1)            同理,第n-2个月的兔子对数该如何表示呢?f(n-2)        StackOverflowError:当堆栈溢出发生时抛出一个应用程序递归太深     */    public static int f(int n) {        if(n==1 || n==2) {            return 1;        } else {            return f(n - 1) + f(n - 2);        }    }}
  • 阶乘
  • public class DiGuiDemo01 {    public static void main(String[] args) {        //调用方法        int result = jc(5);        //输出结果        System.out.println("5的阶乘是:" + result);    }    //定义一个方法,用于递归求阶乘,参数为一个int类型的变量    public static int jc(int n) {        //在方法内部判断该变量的值是否是1        if(n == 1) {            //是:返回1            return 1;        } else {            //不是:返回n*(n-1)!            return n*jc(n-1);        }    }}
  • 流概述

    • 概念:数据传输的总成
    • 分类:   输入流   输出流
    • 工作方式:字节流 字符流
    • IO流的使用场景

      • 如果操作的是纯文本文件,优先使用字符流
      • 如果操作的是图片、视频、音频等二进制文件。优先使用字节流
      • 如果不确定文件类型,优先使用字节流。字节流是万能的流

  • 字节流写数据


      • 字节输出流:OutPutStream() FileOutPutStream("文件的路径")

    • 字节形式写数据:write() write(byte[]) write(byte[],off,len)
    • 释放资源:close()

  • 字节流写数据的两个小问题

    • 换行:/r/n
    • 如何进行追加写:new FileOutPutStream("文件的路径",true)
    • finally try catch后必须要执行的代码块,做关闭资源的操作

  • 字节流读取数据

    • 字节输入流对象:InputStream() FileInputStream("")
    • 读数据的方法:read()     当-1时代表文件到结尾   read(byte[])返回值实际读取数据的长度,-1代表数据读取完成
    • 释放资源:close()
    • Map集合

      • map集合的概述和特点

        • 健值对方式存储数据
        • 一个键对应一个值
        • 健不允许重复,值可以重复
        • 无序

      • map集合常用方法

        • 添加:put(key,value)
        • 删除:remove(key)
        • 清除:clear()
        • 判断健是否存在:containsKey(key)
        • 判断值是否存在:containsValue(value)
        • 是否为空:isEmpty()
        • 长度:size()

      • map结合读取数据方法

        • 获取单个元素:get(key)
        • 获取所有健:keySet()
        • 获取所有的值:values()
        • 获取健和值的集合:entrySet()      Map.Entry<k,v>

      • Collections常用方法

        • 排序:sort()
        • 反转:reverse()
        • 洗牌:shuffle()
        • [Java] 纯文本查看 复制代码
          public class PokerDemo {
              public static void main(String[] args) {
                  //创建HashMap,键是编号,值是牌
                  HashMap<Integer, String> hm = new HashMap<Integer, String>();
          
                  //创建ArrayList,存储编号
                  ArrayList<Integer> array = new ArrayList<Integer>();
          
                  //创建花色数组和点数数组
                  String[] colors = {"♦", "♣", "♥", "♠"};
                  String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
          
                  //从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
                  int index = 0;
          
                  for (String number : numbers) {
                      for (String color : colors) {
                          hm.put(index, color + number);
                          array.add(index);
                          index++;
                      }
                  }
                  hm.put(index, "小王");
                  array.add(index);
                  index++;
                  hm.put(index, "大王");
                  array.add(index);
          
                  //洗牌(洗的是编号),用Collections的shuffle()方法实现
                  Collections.shuffle(array);
          
                  //发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
                  TreeSet<Integer> lqxSet = new TreeSet<Integer>();
                  TreeSet<Integer> lySet = new TreeSet<Integer>();
                  TreeSet<Integer> fqySet = new TreeSet<Integer>();
                  TreeSet<Integer> dpSet = new TreeSet<Integer>();
          
                  for (int i = 0; i < array.size(); i++) {
                      int x = array.get(i);
                      if (i >= array.size() - 3) {
                          dpSet.add(x);
                      } else if (i % 3 == 0) {
                          lqxSet.add(x);
                      } else if (i % 3 == 1) {
                          lySet.add(x);
                      } else if (i % 3 == 2) {
                          fqySet.add(x);
                      }
                  }
          
                  //调用看牌方法
                  lookPoker("林青霞", lqxSet, hm);
                  lookPoker("柳岩", lySet, hm);
                  lookPoker("风清扬", fqySet, hm);
                  lookPoker("底牌", dpSet, hm);
              }
          
              //定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
              public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
                  System.out.print(name + "的牌是:");
                  for (Integer key : ts) {
                      String poker = hm.get(key);
                      System.out.print(poker + " ");
                  }
                  System.out.println();
              }
          }



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马