Date

Date

SimpleDateFormat 日期格式转换类|器

格式:
手动设置 new SimpleDateFormat(“转换格式模板”)
y 年
M 月
d 日期
H 时 24小时
hh 12小时制
m 分钟
s 秒
S 毫秒

​ 使用SimpleDateFormat默认格式 SimpleDateFormat()

方法:
format(Date) 日期对象转为字符串
parse(String) 字符串转为日期对象

public class SimpleDateFormatDemo02 {
    public static void main(String[] args) throws ParseException {
        //模板转换器
        SimpleDateFormat format = new SimpleDateFormat();  //默认格式
        //日期对象转为字符串
        System.out.println(format.format(new Date()));;
        //字符串转为日期对象
        System.out.println(format.parse("1988/4/7 上午10:01"));

        //指定格式转换模板
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");  //默认格式
        //日期对象转为字符串
        System.out.println(simple.format(new Date()));;
        //字符串转为日期对象
       System.out.println(simple.parse("2021-04-07 10:07:59 184"));
    }
}

JAVA8新增系列时间日期API
优点:
线程安全
操作简单,效率高

LocalTime:代表时间,可以通过时、分、秒来构造
LocalDate:代表日期,可以通过年、月、日构造,并且年/月/日的值都是符合日常使用习惯的
LocalDateTime:表示日期和时间,可以通过年、月、日、时、分、秒来构造,

public class DateDemo001 {
    public static void main(String[] args) {
        //今年
        System.out.println("今年"+ Year.now());
        System.out.println("指定年" + Year.of(2022));
        //6月
        System.out.println("月份:"+ Month.FEBRUARY);
        System.out.println("月份:"+ Month.of(12));

        //今天不包含时分秒
        LocalDate today = LocalDate.now();
        System.out.println("今天:"+today);

        //您的生日
        LocalDate yourBirthDate = LocalDate.of(1999, Month.JUNE, 15);
        //LocalDate yourBirthDate = LocalDate.of(1999, 6, 15);
        System.out.println("生日:"+yourBirthDate);


        //此时此刻
        LocalDateTime now = LocalDateTime.now();
        System.out.println("现在:"+now);


        //您的学习时间
        LocalDateTime dateTime = LocalDateTime.of(2020, 2, 25, 12, 30,40);
        System.out.println("时间:"+dateTime);

        //日期转换器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //格式化日期字符串
        LocalDateTime now2 = LocalDateTime.now();
        String nowStr = now2.format(formatter); //放入格式器
        System.out.println(nowStr);

        String dateStr= "2020-02-25 11:23:04";
         //转成日期
        LocalDateTime date= LocalDateTime.parse(dateStr, formatter);//放入格式器
        System.out.println(date);
         //获取日
        System.out.println(date.getDayOfMonth());
        System.out.println(date.getYear());

        LocalDate today3 = LocalDate.now();
        LocalDate birthDate = LocalDate.of(2001, 4, 7);
        //时期间隔 年月日
        Period p = Period.between(birthDate, today3);
        System.out.printf(p.getYears()+"年"+p.getMonths()+"月"+p.getDays()+"日");

        LocalDate startDate = LocalDate.of(1993, 8, 19);
        LocalDate endDate = LocalDate.of(1994, Month.JANUARY,16);
        //期量单位 间隔
        long between = ChronoUnit.YEARS.between(startDate, endDate);
        System.out.println("两年之间的差 : " + between); //0 不是1不满一年不计算在内
        between =ChronoUnit.MONTHS.between(startDate, endDate);
        System.out.println("两月之间的差 : " + between); //4 不是5不满一月不计算在内
        between =ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("两天之间的差 : " + between); //150

        //瞬间
        //不包含时区  小时: 差8小时
        Instant inst1 = Instant.now();
        System.out.println(inst1);
        Instant inst2 = inst1.minus(Duration.ofSeconds(3));
        System.out.println(inst2);
        System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toMillis());
        System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toSeconds());
    }
}

File

File
文件目录的抽象的表现形式(不确定系统中是否真实存在)

public class FileDemo01 {
    public static void main(String[] args) throws IOException {
        /*
        File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
        File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
        File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
         */
        File file = new File("D://test.txt");
        File file2 = new File("D:/hahaha.txt");
        File file3 = new File("D:\\test.txt");
        File file4 = new File("D://AAA");
        File file5 = new File("test.txt");
        File file6 = new File(file4,"test.txt");
        File file7 = new File("D://AAA","test.txt");

        System.out.println(file);
        System.out.println(file2);
        System.out.println(file3);
        System.out.println(file4);
        System.out.println(file5);
        System.out.println(file6);
        System.out.println(file7);

        //常用方法
        //boolean setReadOnly() 标记此抽象路径名指定的文件或目录,以便仅允许读取操作。
        //System.out.println(file.setReadOnly());
        //boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。
        //System.out.println(file.canWrite());
        //System.out.println(file.canRead());
        //System.out.println(file.canExecute());

        //boolean createNewFile() 当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。
        //目录需要存在才能创建指定的文件 IOException:系统找不到指定的路径。
        System.out.println(file.exists());
        if(!file.exists()){
            System.out.println(file.createNewFile());
        }
        //System.out.println(file7.createNewFile());

        //boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
        //boolean delete() 删除此抽象路径名表示的文件或目录。
        System.out.println(file2.delete());

        //File getAbsoluteFile() 返回此抽象路径名的绝对形式。
        //String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
        System.out.println(file5.getAbsoluteFile());  //如果想对file路径,默认绝对路径为当前项目所在路径,不包含src
        System.out.println(file5.getAbsolutePath());

        System.out.println(file5.createNewFile());

    }
}
public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        File file = new File("src/haha.txt"); //默认在当前项目下+相对路径
        File file2 = new File("/src/haha.txt");  //默认项目在盘符+想对路径
        System.out.println(file.getAbsolutePath());
        System.out.println(file2.getAbsolutePath());

        File file3 = new File("D://");
        File file4 = new File("D://AAA/test.txt");
        File file5 = new File("D://AAA");
        System.out.println(file3.getFreeSpace());  //空暇字节数
        //String getName() 返回此抽象路径名表示的文件或目录的名称。
        System.out.println(file4.getName());
        System.out.println(file5.getName());

        //String getParent() 返回此抽象路径名父项的路径名字符串,如果此路径名未指定父目录,则返回 null 。
        //File getParentFile() 返回此抽象路径名父项的抽象路径名,如果此路径名未指定父目录,则返回 null 。
        System.out.println(file4.getParent());
        System.out.println(file4.getParentFile());

        //long getTotalSpace() 通过此抽象路径名返回分区 named的大小。
        System.out.println(file3.getTotalSpace());

        //boolean isAbsolute() 测试此抽象路径名是否为绝对路径。
        System.out.println(file5.isAbsolute());

        //boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。
        //boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。
        System.out.println(file5.isDirectory());
        System.out.println(file5.isFile());

        //long lastModified() 返回上次修改此抽象路径名表示的文件的时间。
        System.out.println(new SimpleDateFormat().format(new Date(new File("D://test.txt").lastModified())));;

        //long length() 返回此抽象路径名表示的文件的长度。
        File f = new File("D://test.txt");
        System.out.println(f.length());  //字节数

        //String[] list() 返回一个字符串数组,用于命名此抽象路径名表示的目录中的文件和目录。
        //File[] listFiles() 返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。
        System.out.println(Arrays.toString(file5.list()));
        System.out.println(Arrays.toString(file5.listFiles()));

        //boolean mkdir() 创建此抽象路径名指定的目录。
        //boolean mkdirs() 创建此抽象路径名指定的目录,包括任何必需但不存在的父目录。 只能创建目录,不能创建文件
        File f2 = new File("D://haha/hehe/heihei.txt");
        //System.out.println(f2.mkdirs());
        System.out.println(f2.getParentFile().mkdirs());
        System.out.println(f2.createNewFile());;

        //boolean renameTo(File dest) 重命名此抽象路径名表示的文件。  最好操作同一盘符下  改名|移动
        //System.out.println(f.renameTo(new File("D://hehe.txt")));;

        System.out.println(new File("D://hehe.txt").renameTo(new File("D://haha/hehe.txt")));;

 

    }
}

io流

file类能够操作文件外部得到内容,无法操作文件内容的内容数据,如果想要操作文件内部的内容通过IO流
IO 流: (文件内容的读入写出,文件的拷贝,文件夹的拷贝,上传与下载…)
流: 就是管道
用来传输数据
数据是以先入先出的形式流动传输

​ 数据源: 数据的源头
​ 目的地: 数据的传输位置
​ 中心: 人(大脑),程序为中心

​ 流的分类:
​ 流向分:
​ 输入流
​ 输出流
​ 操作单元:
​ 字节流 : 万能流
​ 字符流 : 只能操作纯文本内容
​ 功能分:
​ 节点流 : 真是能够做读入写出的流
​ 功能流 : 增强流

​ 流的分类都是相辅相成的!!!

​ 字节流 : 万能流
​ 节点流
​ 字节输入流 : InputStream
​ 字节输出流

​ 功能流

InputStream 抽象父类,所有字节输入流的父类
read… close()

FileInputStream 从文件系统中的文件获取输入字节。 流向: 输入 操作单元:字节流 功能:节点流

public class IODemo01 {
    public static void main(String[] args) throws IOException {
        //FileInputStream(File file)
        //FileInputStream(String name)
        File src = new File("D:\\AAA\\test.txt");
        //1.构建流
        InputStream is = new FileInputStream(src);  //不用子类FileInputStream类型独有内容时候
        //2.读入  read() 每次读取一个字节
        int num = is.read();
        System.out.println((char)num);
        System.out.println((char)(is.read()));
        System.out.println((char)(is.read()));
        System.out.println(is.read());  //如果读取不到数据返回-1

        //3.关闭
        is.close();
    }
}

字节流实现文件拷贝

通过字节流实现文件拷贝
通过程序做中转站
数据源文件–>字节输入流–>程序–>字节输出流–>目的地文件

​ 1.构建流 输出,输出
​ 2.先读入,然后写出
​ 3.输出流刷出
​ 4.关闭流
​ 后打开的先关闭

public class CopyFile06 {
    public static void main(String[] args) throws IOException {
        // 1.构建流  输出,输出
        InputStream is = new FileInputStream("D://test.txt");
        OutputStream os = new FileOutputStream("D://newtest.txt");
        // 2.先读入,然后写出
        //卡车
        byte[] car = new byte[1024];
        //重读读入写出
        int len = -1; //读入到卡车中数据的个数
        while((len=is.read(car))!=-1){
            //读入到多少数据就写出多少数据
            os.write(car,0,len);
        }
        // 3.输出流刷出
        os.flush();
        // 4.关闭流
        os.close();
        is.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值