版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33621967/article/details/60322819
java–IO流hello版回顾之前学习的知识点
package cn.hncu.io.hello;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import org.junit.Test;
public class ReadWriteHello {
public static void main(String[] args) {
//readDemo();
writeDemo();
}
private static void writeDemo() {
FileOutputStream out = null;
//写文件
try {
out = new FileOutputStream("d:/a/files/a_utf8.txt");
String str = "我是中国人,我为woshi一名中国工产党员感到自豪!";
out.write(str.getBytes("gbk"));//写文件时指定码表进行编码
} catch (FileNotFoundException e) {
System.out.println("文件没找到!");
} catch (IOException e) {
System.out.println("文件写失败了!");
}finally{//IO流中最后一定要记得进行关流操作
try {
out.close();
} catch (IOException e) {
throw new RuntimeException("写文件流关闭失败!", e);
}
}
}
@SuppressWarnings("unused")
private static void readDemo() {
byte[] buf = new byte[512];
FileInputStream in = null;
try {
in = new FileInputStream("d:\\a\\files\\a.txt");
in.read(buf);
//下面这种方法是读取不出来的,因为一个汉字对应的字节数不止一个
// for(byte b:buf){
// System.out.print((char)(b));
// }
String str = new String(buf,"gbk");//读取文件并且指定编码,解码
System.out.println(str);
} catch (FileNotFoundException e) {
System.out.println("文件没有找到!");
} catch (IOException e) {
System.out.println("文件读取失败!");
}finally{//IO流中最后一定要记得进行关流操作
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("读取流关闭失败!", e);
}
}
}
//演示老师今天讲的知识点!---学校的javaEE课程
@Test
public void time(){
//玩转时间的三个类----Date,Calendar,DateFormate(由于DateFormat为抽象类,所以我们一般用它的直接子类SimpleDateFormate)
long time1 = new Date().getTime();
Date d = new Date();
System.out.println("当前时间对象:"+d);//Mon Feb 27 19:13:32 CST 2017输出的时间对象
System.out.println("一共经过了多少毫秒:"+d.getTime());//拿到1970年1月1日00:00:00 000到当前时间的毫秒值
//当前这个Date对象里面的方法很多已经过时了,我们现在用另外一个类来进行操作--Calendar类操纵时间
Calendar cal = Calendar.getInstance();//由于Calendar中的构造方法权限是protected受保护的,而我们知道protected的权限,所以我们只能通过工厂方法来造Calendar类对象
int y = cal.get(Calendar.YEAR);//获得当前电脑系统中右下角位置的日历信息
int m = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(y+"年"+m+"月"+day+"日"+"某一天"+Calendar.DATE);
//下面演示第三个类--SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss SSS");//指定日期格式
String str = sdf.format(new Date());
System.out.println(str);
long time2 = new Date().getTime();
System.out.println("程序一共运行了多长时间:"+(time2-time1));//单位为毫秒值
JOptionPane.showMessageDialog(null, "呵呵哒");
//解下来进行演示某个人(这里我用自己来代替实现)从出生到今天共过了多少天!---好像底层是通过毫秒数进行计算的吧!
Date date = new Date();
//拿到当前时间---从1970.1.1. 00:00:00 000时间到现在一共经过多少毫秒数
long mil = date.getTime();
//String birth = "1995/12/10 00:00:00 000";//7751天
String birth = "2017/2/25 00:00:00 000";
//使用SimpleDateFormat类将birth进行解析!
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss SSS");
try {
Date dd = sdf2.parse(birth);
long mil2 = dd.getTime();//拿到这个人的从1970.1.1 00:00:00 000到这个人出生的毫秒值拿到
System.out.println("这个人从出生到现在一共经历了"+((mil-mil2)/(24*60*60*1000))+"天");
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, "当前日期解析失败!");
}
}
}