Java学习记录(补充八:Date类;Java流(Stream),文件(File)和IO)

Posted 天上一片红

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学习记录(补充八:Date类;Java流(Stream),文件(File)和IO)相关的知识,希望对你有一定的参考价值。

Date类,Calendar类
package Box1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random;
//Date类 public class DateTest { public static void main(String[] args) { Date date = new Date(); System.out.println(date); //System.out.println(date.getYear()); //格式化时间 SimpleDateFormat sf= new SimpleDateFormat("yyyy年MM-dd hh:mm:ss E"); String dateString = sf.format(date); System.out.println(dateString); //练习:写一个方法,自动生成定单编号,规则:前2个字符为je //后边14位为当前日期及时间,后面是四位随机数. //例如:je201708291456201890 SimpleDateFormat change = new SimpleDateFormat("yyyyMMddhhmmss"); String changeDate = change.format(date); Random a = new Random(); int a1 = a.nextInt(10); int a2 = a.nextInt(10); int a3 = a.nextInt(10); int a4 = a.nextInt(10); System.out.println("je"+changeDate+a1+a2+a3+a4); //Calendar是抽象类,不能实例化, //能通过调用静态方法来实例化,用来产生对象的静态方法,一般称为工厂方法 Calendar c= Calendar.getInstance(); System.out.print(c.get(Calendar.YEAR)+"-"); System.out.print(c.get(Calendar.MONTH)+1+"-"); System.out.print(c.get(Calendar.DATE)+" "); System.out.print(c.get(Calendar.HOUR)+":"); System.out.print(c.get(Calendar.MINUTE)+":"); System.out.print(c.get(Calendar.SECOND)+" "); System.out.println(c.get(Calendar.DAY_OF_WEEK));//星期日是1 //date与calendar互转 Date d1 = c.getTime(); System.out.println(sf.format(d1)); c.setTime(date); //日期的计算 Calendar c1 = Calendar.getInstance(); //第一个参数:要加那个字段 //第二个参数:要加多少 c1.add(Calendar.DATE, 100); Date d2 = c1.getTime(); System.out.println(sf.format(d2)); //日期与字符串互转 //Date转String用SimpleDateFormat Date d3 = new Date("2017/8/9");//格式不对会报错 long d4= Date.parse("2017/10/10"); Date d5 = new Date(); d5.setTime(d4); //把字符串转成Date SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date d6=format.parse("2017-08-09"); System.out.println(d6); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar c2 = Calendar.getInstance(); c2.set(2017, 9, 27, 20, 20,12); } }

结果图:

 

枚举
package Box1;
//枚举:表示常量,提高代码的可读性 public enum Week { MON,TUE,WED,THU,SAT,SUN }
package Box1;

public class WeekTestEnum {
    public static void main(String[] args) {
            System.out.println(Week.MON);
            Week n =Week.MON;
            switch (n) {
            case MON:
                System.out.println("周一");
                break;
            case TUE:
                System.out.println("周二");
                break;
            default:
                break;
            }
            
        }
}

结果图:

 

 

单例模式(重点)

 

 

 

 

Java流(Stream),文件(File)和IO

package com.jredu.iodemo;
//Java 流(Stream)、文件(File)和IO
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOTest {
    public static void main(String[] args) {
        try {
            FileInputStream fis=
                    new FileInputStream("G:/IO数据/io.text.txt");//调用文档
            //磁盘目录下的文档
            byte[]data = new byte[1024];
            int len;
            while((len=fis.read(data))!=-1){
                String s=new String(data,0,len);
                System.out.println(s);
            }
            fis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            FileOutputStream fos = 
                    new FileOutputStream("G:/IO数据/io.text.txt");
            fos.write("abc".getBytes());//直接覆盖里面的内容
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
    }
}

结果图::

package com.jredu.iodemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyDemo {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("G:/IO数据/123.png");
            FileOutputStream fos = new FileOutputStream("G:/IO数据/1234.png");
            
            byte []data = new byte[1024];
            int len;
            try {
                while((len=fis.read(data))!=-1){
                    fos.write(data,0,len);
                }
                fis.close();
                fos.flush();
                fos.close();
                System.out.println(" copy OK!");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

结果图:

 

以上是关于Java学习记录(补充八:Date类;Java流(Stream),文件(File)和IO)的主要内容,如果未能解决你的问题,请参考以下文章

Java学习笔记4.5.1 日期时间 - Date类与Calendar类

Java IO流学习总结八:Commons IO 2.5-IOUtils

Java IO流学习总结八:Commons IO 2.5-IOUtils

[ C++ ] 类与对象(下)日期类Date补充及流提取和流插入运算符重载

八月份第四周学习总结

Java学习记录(补充三:面向对象的封装和继承)