学习Java第五周

一、File类

1.相对路径和绝对路径

相对路径:

./ 代表的是当前的工作目录

…/ 上一级目录

…/…/ 上两级目录

…/…/…/ 上三级目录

绝对路径:

从磁盘的根目录一直到文件的所在位置

2.File构造方法

通过将给定的路径名字符串转换为抽象路径名来创建新的File实例。

FileString pathname)
    
    
//将文件和文件夹路径抽象成了一个对象
        File file = new File("c:/aaa/3.txt");
        System.out.println(file);//c:\aaa\3.txt

从 父抽象路径名 和 子路径名字符串创建新的File实例。

FileString parent,String child)


File file1 = new File("c:/aaa", "3.txt");
        System.out.println(file1);

分隔符字符

static Stringseparator
与系统相关的默认名称—分隔符字符
File file3 = new File("c:" + File.separator + "aaa" + File.separator + "3.txt");
        System.out.println(file3);

3.File类下面的方法

boolean createNewFile(); 新建一个文件

如果文件已经存在,返回false

 File file = new File("c:/aaa/sb.txt");
 System.out.println(file.createNewFile());//创建新文件

boolean mkdir(); 创建单级的目录

 File file1 = new File("c:/aaa/bbb");
 System.out.println(file1.mkdir());//创建单级目录

boolean mkdirs(); 创建多级的目录

 File file2 = new File("c:/aaa/ccc/ddd");
 System.out.println(file2.mkdirs());//创建多级目录

boolean delete(); 删除文件或者文件夹

delete只能删除空的文件夹或者文件

File file3 = new File("c:/aaa/ccc");
        System.out.println(file3.delete());//因为ccc下面还有文件夹或者文件
        //delete只能删除空的文件夹或者文件

File类下面判断的方法

boolean isFile();是否是文件

boolean isDirectory(); 是否是文件夹

boolean isHidden(); 是否是隐藏文件

boolean isAbsolute(); 是否是绝对路径

boolean exist(); 文件或者文件夹是否存在

获取方法 返回值的是字符串的方法

String getName();获取文件或者文件夹的名字的

STring getParent(); 获取上一级目录

String getPath(); 获取file对象的绝对路径

方法是long类型数据

long length(); 获取文件占用磁盘的大小 是一个字节数

long lastModified(); 获取当前文件修改的时间 时间戳

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数

获取当前文件夹下面的所有的文件

File[] listFiles();

String[] list();

 public static void main(String[] args) {
        File file = new File("c:/");
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(file1);//获取文件对象
        }

        System.out.println("=======");

        String[] list = file.list();
        for (String s : list) {
            System.out.println(s);//获取名字而已
        }
    }

二、递归

一句话来概括:

一个方法自己多次调用自己,但是得有结束的条件

在递归调用的过程中,系统为每一层的返回点或者局部变量开辟了栈来存储

public class Demo3 {
    public static void main(String[] args) {
        File file = new File("c:/bbb");
        del(file);
    }
    public static void del (File file) {
        //找到bbb文件夹下面所有的文件和文件夹
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {//如果是文件夹 就继续执行del方法
                del(file1);
            } else {//不是文件夹
                file1.delete();

            }
        }
    }
}

三、IO流

1.电脑上面的文件,在进行读取和存储的时候,都是以流的形式进行操作的。 具体需求就是 上传和下载

2.缓冲是为了提高读取和存储的效率

计算机通过cpu读取硬盘的数据,在Java中可以加上缓冲的概念,每次读取具体的缓冲值。可以提高效率

1.字节输入输出流

字节数组 byte【】

读取原始字节流(图片 音频 视频)用字节流

  public static void main(String[] args) throws IOException {
        copyVideo1();
    }
    public static void copyVideo () throws IOException {
        //创建缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("c:/bbb/12转义字符.mp4")));
        //创建缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("c:/aaa/sb.mp4")));
        //准备一个缓冲数组
        byte[] buf = new byte[4096];
        int length;
        while ((length = bis.read(buf)) != -1) {//length = bis.read(buf)  从磁盘读取数据到缓冲数组中
            bos.write(buf, 0, length);//从缓冲数组中写入到磁盘
        }
        bos.close();
        bis.close();
    }
    //不带缓冲的
    public static void copyVideo1 () throws IOException {
       FileInputStream fis = new FileInputStream(new File("c:/bbb/12转义字符.mp4"));
       FileOutputStream fos = new FileOutputStream(new File("c:/aaa/2b.mp4"));
       int length;
       while ((length = fis.read()) != -1) {
            fos.write(length);
       }
       fos.close();
       fis.close();

    }

2.字符输入输出流【了解】

字符数组 char【】

 public static void main(String[] args) throws IOException {
        //字符流将一个文本赋值2到另外一个文件夹下面
        //BufferedReader  专门将文本内容读取到内存中
        BufferedReader br = new BufferedReader(new FileReader("c:/bbb/《雪中悍刀行》.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("c:/aaa/88.txt"));
        char[] cbuf= new char[1024];
        int length;
        while ((length = br.read(cbuf)) != -1) {
            bw.write(cbuf, 0, length);
        }
        bw.close();
        br.close();
    }

四、序列化和反序列化

新建类 然后创建对象,对对象的属性赋值。对象真实存在,然后可以通过流将对象存到本地磁盘上,这叫序列化。(从内存到磁盘)

本次磁盘上面存有对象的数据,可以再通过流读取到Java代码中变成对象 ,这叫反序列化。(从磁盘到内存)

以上两种操作是持久性操作

1.序列化操作

输出流 ObjectOutoutStream writeobject

class Employee implements Serializable {
    String name;
    int age;
    transient int ID;//短暂的  此属性不可序列化
    String adress;//地址

    public void eat() {
        System.out.println("今天中午没有吃饱");
    }
}

public class Demo1 {
    public static void main(String[] args) throws IOException {
        ArrayList arrayList = new ArrayList();
        Employee employee = new Employee();
        employee.name = "gousheng";
        employee.adress = "航海中路";
        employee.age = 18;
        employee.ID = 8989;
        //将Java代码中的对象存到磁盘中
        FileOutputStream fos = new FileOutputStream("c:/aaa/emp.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(employee);
        oos.close();
        fos.close();

    }

2.反序列化操作

输入流 ObjectInputStream readobject

  public static void main(String[] args) throws Exception {
        //反序列化
        FileInputStream fis = new FileInputStream("c:/aaa/emp.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Employee emp = (Employee)ois.readObject();
        System.out.println(emp.name);
        System.out.println(emp.age);
        System.out.println(emp.adress);
        System.out.println(emp.ID);//0
    }

五、StringBuffer

线程安全,可变的字符序列。字符串缓冲区就像一个String,可以修改。在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容。

用这种方式创建字符串:

String str = “abc”; 在常量池中 是abc 地址是0x009

str+= “ef”; 常量池中 会有新的地址 是abcdef 新地址0x008

用StringBuffer创建字符串:

StringBuffer sb = new StringBuffer(“abc”); 是"abc“ 地址是0x007

sb.append(“ef”); 字符串变成 “abcef” 地址没有改0x007

1.StringBuffer方法

append(“a”);追加

insert(2,“gou”);插入

delete(2,4); 删除

reverse();字符串反转

capacity();字符串容量,容量是新插入字符可用的存储量

六、枚举类

1.枚举类的解释

Java中有一个特殊的类叫枚举类,一般表示的是常量。

public static final int A = 23;

枚举就是替换上面常量的写法。

2.枚举的格式

语法格式:

public enum 枚举类名 {
	各组常量,常量之间使用逗号隔开
}
enum Color {
    RED, GREEN, BLUE
}
public class Demo1 {
    public static void main(String[] args) {
        Color red = Color.RED;
        System.out.println(red);//RED
    }
}

有构造方法的创建

enum Ball {
    TYPE_BALL_FOOTBALL(1, "足球"),
    TYPE_BALL_PINGPANGBALL(2, "乒乓球"),
    TYPE_BALL_BASEGETBALL(3, "篮球"),
    ;
    int key;
    String name;

    Ball(int key, String name) {
        this.key = key;
        this.name = name;
    }
    //添加set和get方法
}
public class Demo3 {
    public static void main(String[] args) {
        Ball.TYPE_BALL_FOOTBALL.setKey(4);
        int key = Ball.TYPE_BALL_FOOTBALL.getKey();
        System.out.println(key);
     String name = Ball.TYPE_BALL_FOOTBALL.getName();
        System.out.println(name);
    }
}

3.枚举方法

values(); 枚举类中的所有的值
oridnal();每个常量的索引值
valueOf();返回值的指定的字符串的常量

enum Color1 {
    RED, GREEN, BLUE
}
public class Demo4 {
    public static void main(String[] args) {
        Color1[] values = Color1.values();
        for (Color1 value : values) {
            System.out.println(value +"对应的索引:" + value.ordinal() );
        }
        //valueOf();返回值的指定的字符串的常量
        Color1 red = Color1.valueOf("RED");
        System.out.println(red);
    }
}

4.应用实例

enum SexEnum {
    MALE(0, "男"),
    FEMALE(1, "女"),
    ;
    private int sex;//0代表男 or 1  代表女
    private String sexName;//男 or 女

    SexEnum(int sex, String sexName) {
        this.sex = sex;
        this.sexName = sexName;
    }

    public int getSex() {
        return sex;
    }

    public String getSexName() {
        return sexName;
    }
}
class User {//用户类
    private String name;//用户名字
    private SexEnum sex;//用户的性别

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SexEnum getSex() {
        return sex;
    }

    public void setSex(SexEnum sex) {
        this.sex = sex;
    }
}
public class Demo6 {
    public static void main(String[] args) {
        User user = new User();
        user.setName("狗蛋");//赋值
        user.setSex(SexEnum.MALE);

        System.out.println(user.getSex().getSexName());



    }
}

七、包装类

Java八大基本数据类型,都有与之对应的包装类

为啥会有这些包装类?其实就代表基本数据类型所有东西

包装类能够实例化处理对象,有很多的方法,来处理当前的数据

这样来操作八大基本数据类型很方便

【重点】:

​ 1.自从jdk5之后 ,有自动拆箱和自动装箱

​ 自动装箱: 将基本数据类型转为包装类类型

​ 自动拆箱: 将包装类转为基本数据类型

 		2. static String    toString();  将基本数据类型转为 字符串
 		3. static  parse数据类型(); 将一个字符串转为 所对应基本数据类型
 		4. 数据类型value(); 获得包装类的基本数据
 //1.自动装箱:  将基本数据类型转为包装类型
        int i = 30;
        Integer i1 = i;
        System.out.println(i1.hashCode());
 //2.自动拆箱:  将包装类转为基本数据类型
        int i2 = i1;
        System.out.println(i2);
 //3.***Value();
        Integer i3 = 40;//i3是包装类类型的数据
        int i4 = i3.intValue();//intValue
        System.out.println(i4);//i4是基本数据类型
        //shortValue()

 //4.toString方法
        String s = Integer.toString(34);
        System.out.println(s);

  //5将整数字符串转为基本数据类型
        int i5 = Integer.parseInt("999");
        System.out.println(i5);//999
        double v = Double.parseDouble("89.7");

八、Math类

Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数

 public static void main(String[] args) {
        System.out.println(Math.E);
        System.out.println(Math.PI);

        System.out.println(Math.abs(-89));//求一个数的绝对值  absolute
        System.out.println(Math.max(3, 7));//7   求两个数最大值的
        System.out.println(Math.max(1, Math.max(3, 9)));//9
        System.out.println(Math.min(1, 2));
        System.out.println(Math.ceil(34.5));//向上取整
        System.out.println(Math.floor(34.5));//34.0  向下取整

        System.out.println(Math.round(34.6));//35 long
        System.out.println(Math.random());//double  大于等于 0.0 ,小于 1.0 。
    }

九、Random 随机类

 public static void main(String[] args) {
        Random random = new Random();
        System.out.println(random.nextInt());
        System.out.println(random.nextBoolean());
        System.out.println(random.nextInt(3));
        System.out.println(random.nextDouble());
    }

十、System类

System 类提供的 System 包括标准输入,标准输出和错误输出流; 访问外部定义的属性和环境变量; 一种加载文件和库的方法; 以及用于快速复制阵列的一部分的实用方法。

 public static void main(String[] args) {
        PrintStream out = System.out;//标准输出
        out.println("hehe");
        System.out.println("xiix");
        Scanner scanner = new Scanner(System.in);
        //m	err
        //“标准”错误输出流。
        System.err.println("haha");
        //返回当前的时间
        long l = System.currentTimeMillis();//ms
        System.out.println(l);//1680076586166
//在1970年1月1日UTC之间的当前时间和午夜之间的差异,以毫秒为单位。
        //通过类 获取当前系统的一个属性
        Properties properties = System.getProperties();
        System.out.println(properties.get("os.name"));
        //Windows 10
       System.out.println(properties.get("user.name"));
        //bowang
    System.out.println(properties.get("java.version"));
        //1.8.0_241
    }

十一、Date 日期类

专门处理时间的类

1、格式化时间

SimpleDateFormat

		Date date = new Date();
        System.out.println(date);
        //Wed Mar 29 16:17:57 IRKT 2023
        //SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期
        //SimpleDateFormat(String pattern)
        //使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        System.out.println(format);

2.日期类的方法

public static void main(String[] args) {
        Date date = new Date();
    //返回一个值,该值是从包含本开始时间的年份中减去1900的值
      System.out.println(date.getYear() + 1900);
        //返回一个数字,表示包含或开始于此Date对象所代表的时刻的月份 。 返回的值在0和11之间,其值为0代表一月。
        System.out.println(date.getMonth() + 1);//2
        //返回由此日期表示的星期几。 返回值( 0 =星期日, 1 =星期一, 2 =星期二, 3 =星期三, 4 =星期四, 5 =星期五, 6 =星期六)表示包含或以此时间表示的时刻开始的星期几Date对
        System.out.println(date.getDay());//3

        Calendar rightNow = Calendar.getInstance();//获取日历的对象
        System.out.println(rightNow);
 System.out.println(rightNow.get(Calendar.YEAR));
//2023
System.out.println(rightNow.get(Calendar.MONTH) +1);
//3 
System.out.println(rightNow.get(Calendar.DAY_OF_MONTH));//29    
System.out.println(rightNow.get(Calendar.DAY_OF_YEAR));//88     
System.out.println(rightNow.get(Calendar.DAY_OF_WEEK) - 1);//4     
System.out.println(rightNow.get(Calendar.HOUR));
//4  pm       
System.out.println(rightNow.get(Calendar.HOUR_OF_DAY));//16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值