一、File类

1.1File类的构造方法

  1. File file = new File("文件的路径");
  2. windows系统和Linux系统中,路径用到不同的正斜线和反斜线,因此可以使用一个File.separator来代表斜线,可以自动识别系统需要的斜线
  3. 用法:File f = new File("E:" + File.separator + "aaa");

1.2使用对象创建文件和文件夹

  1. 创建文件:File f1 = new File("文件路径");
  2. sout(f1.creatNewFile());//返回值boolean
  3. 创建单级路径:boolean mkdir();创建单级路径
  4. boolean mkdirs();创建多级的路径

1.3删除文件或文件夹

  1. boolean delete();立即删除文件或文件夹(如果是文件夹的时候,文件夹必须是空的才能删)
  2. void deleteOnExit();程序结束时才删除
  3. File file = new File("文件路径");
  4. file.deleteOnExit();//程序退出时删除

1.4一些方法

  1. boolean isFile();//是否为文件
  2. boolean isDirectory();//是否为文件夹
  3. boolean isHidden();//是否为隐藏文件
  4. boolean isAbsolute();//是否为绝对路径
  5. boolean exists();//对应的文件或文件夹是否存在
  6. String getName();//获取文件的名字
  7. String getPath();//获取文件的路径
  8. String getParent();//获取当前文件的上级目录
  9. String getAbsoluPath();//获取该文件的绝对路径
  10. long lastModified();//获取该文件最后一次修改的时间,(距离1970.1.1的毫秒数)【将毫秒数转换为时间】
  11. File[] listFiles();//将路径下的文件和文件夹存到数组中【删除文件夹下的所有文件】

将毫秒数转换为时间

  1. long time = file.lastModified();
  2. SimpleDateFormat sdf = new new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式可自己调整
  3. String string =sdf.format(time);

删除文件夹下的所有文件【递归】

  1. main{
  2. File file = new File("e:/aaa");
  3. digui(file);
  4. }
  5. public static void digui(File f){
  6. File[] files = f.lastFiles();//变成文件数组
  7. for(File f1 : files){//增强for循环遍历
  8. if(f1.isDirectory){//如果是文件夹
  9. digui(f1);//再次调用改方法
  10. }else{
  11. f1.delete();//否则就是文件,直接删除
  12. }
  13. }
  14. }

二、IO流

2.1IO流的理解

IO代表这intput和output,输入和输出。

什么是输入?从电脑磁盘输入到java内存地址的叫输入。

什么是输出?从java内存地址输出到电脑磁盘的是输出。

2.2字节输入流流程

  1. 1.实例化一个文件对象
  2. File file = new File("文件路径");
  3. 2.使用自己输入流
  4. FileIntputStream fis = new FileIntputStream(file);
  5. 3.加一个缓冲流,增加效率
  6. BufferedIntputStream bis = new BufferedIntputStream(fis);
  7. 4.准备一个缓冲区 字节数组byte[] 一般都是4kb即可
  8. byte[] bytes = new byte[1024 * 4];
  9. 5.while循环读取,因为不知道读取多少次,所以终止条件是length = -1
  10. int length = -1;
  11. while((length = bis.read(bytes)) != -1){//读取到字节数组中
  12. sout(length);//输出字节数
  13. sout(new String(bytes,0,length));//输出内容
  14. //new String(字节数组,偏移量,数量)
  15. }
  16. 6.最后结束时需要关闭流,后开的先关
  17. bis.close();
  18. fis.close();

2.3字节输入流程

  1. 1.创建一个被写入的文件对象
  2. File file = new File("E:/aaa/3.txt");
  3. 2.字节输出流
  4. FileOutputStream fos = new FileOutputStream(file);
  5. 3.缓冲输出流
  6. BufferedOutputStream bos = new BufferedOutputStream(fos);
  7. 4.将字符串写入磁盘中
  8. String string = "输入的内容";
  9. byte[] bytes = string.getBytes();
  10. bos.write(bytes);
  11. 5.关闭流 先关后开的流
  12. bos.close();
  13. fos.close();

2.4输入输出流同时使用案例

复制一个文件到另一个磁盘,以及两种方法,看看缓冲流的效果

  1. package com.qfedu.test2IOliu;
  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. //复制图片
  10. public class Demo5anli2 {
  11. public static void main(String[] args) throws IOException {
  12. copyPhoto();
  13. copyPhoto1();
  14. }
  15. public static void copyPhoto() throws IOException {
  16. long start = System.currentTimeMillis();//开始时间 计算程序运行时间
  17. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\ZHU\\Pictures\\Saved Pictures\\luo.jpeg")));
  18. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/aaa/luo1.png")));
  19. byte[] b1 = new byte[1024 * 4];
  20. int length = -1;
  21. while((length = bis.read(b1)) != -1) {
  22. bos.write(b1);
  23. }
  24. bos.close();
  25. bis.close();
  26. System.out.println("复制完成");
  27. long end = System.currentTimeMillis();//结束时间
  28. System.out.println(end - start);//计算代码运行所用得时间 检查效率
  29. }
  30. public static void copyPhoto1() throws IOException {
  31. long start = System.currentTimeMillis();//开始时间 计算程序运行时间
  32. FileInputStream fis = new FileInputStream(new File("C:\\Users\\ZHU\\Pictures\\Saved Pictures\\luo.jpeg"));
  33. FileOutputStream fos = new FileOutputStream(new File("E:/aaa/luo2.png"));
  34. //不用缓冲流
  35. int length = -1;
  36. while((length = fis.read()) != -1) {
  37. fos.write(length);
  38. }
  39. fis.close();
  40. fos.close();
  41. System.out.println("复制完成");
  42. long end = System.currentTimeMillis();//结束时间
  43. System.out.println(end - start);//计算代码运行所用得时间 检查效率
  44. }
  45. }

2.4字符输入流输出流案例【不常用】

因为字符存在弊端,只能传输字符相关文件,音频,视频没办法使用

  1. public static void copyFile(){
  2. File file = new File("读取的文件路径");
  3. BufferedReader br = new BufferedReader(new FileReader(file));
  4. BufferedWriter bw = new BufferedWriter(new FileWieter("复制到的新路径"));
  5. char[] ch = new char[4096];
  6. int length = -1;
  7. while((length = br.read(ch)) != -1){
  8. bw.write(ch);
  9. }
  10. bw.close();
  11. br.close();
  12. }

改进,可以一行一行的复制

  1. public static void copyFile(){
  2. File file = new File("读取的文件路径");
  3. BufferedReader br = new BufferedReader(new FileReader(file));
  4. BufferedWriter bw = new BufferedWriter(new FileWieter("复制到的新路径"));
  5. String s1 = null;
  6. while((s1 = br.readLine()) != null){//一行一行的读取,如果不为null的话
  7. bw.write(s1);
  8. bw.newLine();//换行
  9. }
  10. bw.close();
  11. br.close();
  12. }

三、StringBuffer类

使用StringBuffer类进行字符串拼接不改变内存地址

addend():拼接

insert(int a,String s1):在指定位置添加

reverse():倒叙输出

  1. StringBuffer sb = new StringBuffer("abcef");
  2. sout(sb.append("拼接"));//abcef拼接
  3. sout(sb.insert(2,"指定位置"));//ab指定位置cef
  4. sout(sb.reverse());//fecba

四、枚举

枚举在swich-case中的使用

  1. package com.qfedu.test3enum;
  2. //枚举在swich case中的使用
  3. enum Color3{
  4. RED,GREEN,BLUE
  5. }
  6. public class Demo3 {
  7. public static void main(String[] args) {
  8. Color3 color3 = Color3.BLUE;
  9. switch (color3) {
  10. case RED:
  11. System.out.println("红色");
  12. break;
  13. case GREEN:
  14. System.out.println("绿色");
  15. break;
  16. case BLUE:
  17. System.out.println("蓝色");
  18. break;
  19. default:
  20. System.out.println("没有这个颜色");
  21. break;
  22. }
  23. }
  24. }

五、包装类

除了int和char,其他几种数据类型的包装类都是首字母大写,char—>Character。int—>Integer

  1. 【重点】
  2. 装箱:将基本数据类型转为包装类对象
  3. int i1 = 16;
  4. Integer i2 = i1;
  5. valueOf()方法:类似装箱
  6. byte b1 = 56;
  7. Byte b2 = Byte.valueOf(b1);
  8. 拆箱:将包装类对象转为基本数据类型
  9. Integer i1 = 31;
  10. int i2 = i1;
  11. ***Value()方法:类似拆箱
  12. Byte byte1 = 20
  13. byte byte2 = byte1.byteValue();
  14. toString()方法:将基本数据类型转为字符串
  15. int i1 = 50;
  16. String s1 = Integer.toString(i1);
  17. parse***()方法:字符串转基本类型
  18. String s2 = "999";
  19. int i2 = Integer.parseInt(s2);

六、Math

专门用来处理数学公式的一个类

绝对值 Math.abs(-100)

最大值 Math.max(12, 88) Math.max(10, Math.max(20, 30))//三个数比

最小值 Math.min(10, 20)

向上取整 Math.ceil(3.3) 输出4.0

向下取整 Math.floor(3.3) 输出3.0

四舍五入 Math.round(4.3) 返回值int

随机数 Math.random() [0,1)左开右闭 (int)(Math.random()*(100-50)+50)

指数 Math.pow(2, 3) 2的3次方

离最近的整数 Math.rint(1.5) 1.5离1.0和2.0一样远近,如果有两个,返回偶数

七、Random

专门处理随机数的,提供的方法较多

  1. Random random = new random();
  2. sout(random.nextBoolean());//在true和false范围内随机
  3. sout(random.nextInt());//在int范围内生成随机数

八、System

系统类,不能被实例化(没有构造方法),提供了了静态数学和静态方法

  1. System.currentTimeMillis();//从1970.1.1 0:0:0到现在的毫秒数
  2. 获取此电脑的信息
  3. Properties p = System.getProperties();
  4. sout(p.get("os.name"));//系统名字
  5. sout(p.get("os.version"));//系统版本
  6. sout(p.get("user.name"));//系统用户名字
  7. sout(p.get("user.dir"));//当前项目路径
  8. sout(p.get("java.version"));//当前使用的java版本

九、Runtime

获取java运行时的一些信息,不能被实例化(没有构造方法)

  1. Runtime r = Runtime.getRuntime();
  2. //java虚拟机尝试使用的最大内存量,以字节为单位/1024/1024变成M
  3. sout(r.maxMemory()/1024/1024);
  4. sout(r.freeMemory());//java虚拟机可用的内存量
  5. sout(r.totalMemoy());//java虚拟机的内存总量
  6. r.exec("某程序的位置.exe");//打开电脑中的该路径的程序

十、Date和Calendar

Date

  1. Date date = new Date();
  2. sout(date);//输出当前时间,格式比较复杂
  3. sout(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(date));//格式化date日期
  4. sout(date.getYear() + 1900);//当前年份
  5. sout(date.getMonth() + 1);//当前月份
  6. sout(date.getDay());//获取今天是周几 0是星期日 5是星期五
  7. getHours()//时
  8. getMinutes()//分
  9. getSeconds()//秒

Calendar

  1. Calendar c = Calendar.getInstance();
  2. c.get(Calendar.YEAR);//年
  3. c.get(Calendar.MONTH) + 1;//月
  4. c.get(Calendar.DAY_OF_MONTH);//这个月的第几天
  5. c.get(Calendar.DAY_OF_WEEK);//这一周的第几天 周日是第一天 周一第二天
  6. c.get(Calendar.DAY_OF_YEAR);//这一年的第几天
  7. c.get(Calendar.HOUR);//时
  8. c.get(Calendar.MINUTE);//分
  9. c.get(Calendar.SECOND);//秒
  10. Date date = c.getTime();获取当前日期

练习

指定日期到现在的天数

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
  2. String s1 = "2022-04-01 12:00:00";//指定日期
  3. Date date = sdfparse(s1);//将字符串格式日期解析为date类型日期
  4. long dateTime = date.getTime();//date到1970.1.1的毫秒数
  5. Date date1 = new Date();
  6. long todayTime = date1.getTime();//当前日期到1970.1.1的毫秒数
  7. long time = todayTime - dateTime;
  8. sout(time/1000/60/60/24);//除1000变秒 除60变分 除60秒小时 除24变天