一、File类
1.1File类的构造方法
File file = new File("文件的路径");在windows系统和Linux系统中,路径用到不同的正斜线和反斜线,因此可以使用一个File.separator来代表斜线,可以自动识别系统需要的斜线用法:File f = new File("E:" + File.separator + "aaa");
1.2使用对象创建文件和文件夹
创建文件:File f1 = new File("文件路径");sout(f1.creatNewFile());//返回值boolean创建单级路径:boolean mkdir();创建单级路径boolean mkdirs();创建多级的路径
1.3删除文件或文件夹
boolean delete();立即删除文件或文件夹(如果是文件夹的时候,文件夹必须是空的才能删)void deleteOnExit();程序结束时才删除File file = new File("文件路径");file.deleteOnExit();//程序退出时删除
1.4一些方法
boolean isFile();//是否为文件boolean isDirectory();//是否为文件夹boolean isHidden();//是否为隐藏文件boolean isAbsolute();//是否为绝对路径boolean exists();//对应的文件或文件夹是否存在String getName();//获取文件的名字String getPath();//获取文件的路径String getParent();//获取当前文件的上级目录String getAbsoluPath();//获取该文件的绝对路径long lastModified();//获取该文件最后一次修改的时间,(距离1970.1.1的毫秒数)【将毫秒数转换为时间】File[] listFiles();//将路径下的文件和文件夹存到数组中【删除文件夹下的所有文件】
将毫秒数转换为时间
long time = file.lastModified();SimpleDateFormat sdf = new new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式可自己调整String string =sdf.format(time);
删除文件夹下的所有文件【递归】
main{File file = new File("e:/aaa");digui(file);}public static void digui(File f){File[] files = f.lastFiles();//变成文件数组for(File f1 : files){//增强for循环遍历if(f1.isDirectory){//如果是文件夹digui(f1);//再次调用改方法}else{f1.delete();//否则就是文件,直接删除}}}
二、IO流
2.1IO流的理解
IO代表这intput和output,输入和输出。
什么是输入?从电脑磁盘输入到java内存地址的叫输入。
什么是输出?从java内存地址输出到电脑磁盘的是输出。
2.2字节输入流流程
1.实例化一个文件对象File file = new File("文件路径");2.使用自己输入流FileIntputStream fis = new FileIntputStream(file);3.加一个缓冲流,增加效率BufferedIntputStream bis = new BufferedIntputStream(fis);4.准备一个缓冲区 字节数组byte[] 一般都是4kb即可byte[] bytes = new byte[1024 * 4];5.while循环读取,因为不知道读取多少次,所以终止条件是length = -1int length = -1;while((length = bis.read(bytes)) != -1){//读取到字节数组中sout(length);//输出字节数sout(new String(bytes,0,length));//输出内容//new String(字节数组,偏移量,数量)}6.最后结束时需要关闭流,后开的先关bis.close();fis.close();
2.3字节输入流程
1.创建一个被写入的文件对象File file = new File("E:/aaa/3.txt");2.字节输出流FileOutputStream fos = new FileOutputStream(file);3.缓冲输出流BufferedOutputStream bos = new BufferedOutputStream(fos);4.将字符串写入磁盘中String string = "输入的内容";byte[] bytes = string.getBytes();bos.write(bytes);5.关闭流 先关后开的流bos.close();fos.close();
2.4输入输出流同时使用案例
复制一个文件到另一个磁盘,以及两种方法,看看缓冲流的效果
package com.qfedu.test2IOliu;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;//复制图片public class Demo5anli2 {public static void main(String[] args) throws IOException {copyPhoto();copyPhoto1();}public static void copyPhoto() throws IOException {long start = System.currentTimeMillis();//开始时间 计算程序运行时间BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\ZHU\\Pictures\\Saved Pictures\\luo.jpeg")));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/aaa/luo1.png")));byte[] b1 = new byte[1024 * 4];int length = -1;while((length = bis.read(b1)) != -1) {bos.write(b1);}bos.close();bis.close();System.out.println("复制完成");long end = System.currentTimeMillis();//结束时间System.out.println(end - start);//计算代码运行所用得时间 检查效率}public static void copyPhoto1() throws IOException {long start = System.currentTimeMillis();//开始时间 计算程序运行时间FileInputStream fis = new FileInputStream(new File("C:\\Users\\ZHU\\Pictures\\Saved Pictures\\luo.jpeg"));FileOutputStream fos = new FileOutputStream(new File("E:/aaa/luo2.png"));//不用缓冲流int length = -1;while((length = fis.read()) != -1) {fos.write(length);}fis.close();fos.close();System.out.println("复制完成");long end = System.currentTimeMillis();//结束时间System.out.println(end - start);//计算代码运行所用得时间 检查效率}}
2.4字符输入流输出流案例【不常用】
因为字符存在弊端,只能传输字符相关文件,音频,视频没办法使用
public static void copyFile(){File file = new File("读取的文件路径");BufferedReader br = new BufferedReader(new FileReader(file));BufferedWriter bw = new BufferedWriter(new FileWieter("复制到的新路径"));char[] ch = new char[4096];int length = -1;while((length = br.read(ch)) != -1){bw.write(ch);}bw.close();br.close();}
改进,可以一行一行的复制
public static void copyFile(){File file = new File("读取的文件路径");BufferedReader br = new BufferedReader(new FileReader(file));BufferedWriter bw = new BufferedWriter(new FileWieter("复制到的新路径"));String s1 = null;while((s1 = br.readLine()) != null){//一行一行的读取,如果不为null的话bw.write(s1);bw.newLine();//换行}bw.close();br.close();}
三、StringBuffer类
使用StringBuffer类进行字符串拼接不改变内存地址
addend():拼接
insert(int a,String s1):在指定位置添加
reverse():倒叙输出
StringBuffer sb = new StringBuffer("abcef");sout(sb.append("拼接"));//abcef拼接sout(sb.insert(2,"指定位置"));//ab指定位置cefsout(sb.reverse());//fecba
四、枚举
枚举在swich-case中的使用
package com.qfedu.test3enum;//枚举在swich case中的使用enum Color3{RED,GREEN,BLUE}public class Demo3 {public static void main(String[] args) {Color3 color3 = Color3.BLUE;switch (color3) {case RED:System.out.println("红色");break;case GREEN:System.out.println("绿色");break;case BLUE:System.out.println("蓝色");break;default:System.out.println("没有这个颜色");break;}}}
五、包装类
除了int和char,其他几种数据类型的包装类都是首字母大写,char—>Character。int—>Integer
【重点】装箱:将基本数据类型转为包装类对象int i1 = 16;Integer i2 = i1;valueOf()方法:类似装箱byte b1 = 56;Byte b2 = Byte.valueOf(b1);拆箱:将包装类对象转为基本数据类型Integer i1 = 31;int i2 = i1;***Value()方法:类似拆箱Byte byte1 = 20;byte byte2 = byte1.byteValue();toString()方法:将基本数据类型转为字符串int i1 = 50;String s1 = Integer.toString(i1);parse***()方法:字符串转基本类型String s2 = "999";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
专门处理随机数的,提供的方法较多
Random random = new random();sout(random.nextBoolean());//在true和false范围内随机sout(random.nextInt());//在int范围内生成随机数
八、System
系统类,不能被实例化(没有构造方法),提供了了静态数学和静态方法
System.currentTimeMillis();//从1970.1.1 0:0:0到现在的毫秒数获取此电脑的信息Properties p = System.getProperties();sout(p.get("os.name"));//系统名字sout(p.get("os.version"));//系统版本sout(p.get("user.name"));//系统用户名字sout(p.get("user.dir"));//当前项目路径sout(p.get("java.version"));//当前使用的java版本
九、Runtime
获取java运行时的一些信息,不能被实例化(没有构造方法)
Runtime r = Runtime.getRuntime();//java虚拟机尝试使用的最大内存量,以字节为单位/1024/1024变成Msout(r.maxMemory()/1024/1024);sout(r.freeMemory());//java虚拟机可用的内存量sout(r.totalMemoy());//java虚拟机的内存总量r.exec("某程序的位置.exe");//打开电脑中的该路径的程序
十、Date和Calendar
Date
Date date = new Date();sout(date);//输出当前时间,格式比较复杂sout(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(date));//格式化date日期sout(date.getYear() + 1900);//当前年份sout(date.getMonth() + 1);//当前月份sout(date.getDay());//获取今天是周几 0是星期日 5是星期五getHours()//时getMinutes()//分getSeconds()//秒
Calendar
Calendar c = Calendar.getInstance();c.get(Calendar.YEAR);//年c.get(Calendar.MONTH) + 1;//月c.get(Calendar.DAY_OF_MONTH);//这个月的第几天c.get(Calendar.DAY_OF_WEEK);//这一周的第几天 周日是第一天 周一第二天c.get(Calendar.DAY_OF_YEAR);//这一年的第几天c.get(Calendar.HOUR);//时c.get(Calendar.MINUTE);//分c.get(Calendar.SECOND);//秒Date date = c.getTime();获取当前日期
练习
指定日期到现在的天数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");String s1 = "2022-04-01 12:00:00";//指定日期Date date = sdfparse(s1);//将字符串格式日期解析为date类型日期long dateTime = date.getTime();//date到1970.1.1的毫秒数Date date1 = new Date();long todayTime = date1.getTime();//当前日期到1970.1.1的毫秒数long time = todayTime - dateTime;sout(time/1000/60/60/24);//除1000变秒 除60变分 除60秒小时 除24变天
