码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-30 23:44:48      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:case   min   def   覆盖   try   byte   调用   switch   java   

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)

标签:case   min   def   覆盖   try   byte   调用   switch   java   

原文地址:http://www.cnblogs.com/lizuowei/p/7455751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!