1.Date
Date构造函数
public void test01(){
/*
Date()
分配一个 Date对象,并初始化它,以便它代表它被分配的时间,测量到最近的毫秒。
Date(long date)
分配一个 Date对象,并将其初始化为表示自称为“时代”的标准基准时间以后的指定毫秒数,即1970年1月1日00:00:00 GMT。
*/
Date date = new Date();
System.out.println(date);
Long time = 1000*60*60L;
Date d = new Date(time);
System.out.println(d);
}
Date方法
public void test02(){
/*
getTime()
返回自1970年1月1日以来,由此 Date对象表示的00:00:00 GMT的毫秒 数 。
setTime(long time)
设置此 Date对象以表示1970年1月1日00:00:00 GMT后的 time毫秒的时间点。 */
Date date = new Date();
System.out.println(date.getTime());
Long time = 1000*60*60L;
date.setTime(time);
System.out.println(date);
}
DateSimpleFormat日期格式化类(Date—>String & String—>Date)
public void test03() throws ParseException {
/*
SimpleDateFormat()
构造一个 SimpleDateFormat使用默认模式和日期格式符号为默认的 FORMAT区域设置。
SimpleDateFormat(String pattern)
使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。
format(Date date, StringBuffer toAppendTo, FieldPosition pos)
将给定的 Date成日期/时间字符串,并将结果追加到给定的 StringBuffer 。
parse(String text, ParsePosition pos)
从字符串中解析文本以产生一个 Date 。
*/
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat();
String f = sdf.format(date);
System.out.println(f);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf1.format(date);
System.out.println(format);
Date parse = sdf.parse(f);
System.out.println(parse);
Date parse1 = sdf1.parse(format);
System.out.println(parse1);
}
日期工具类
public class Utils {
/*日期工作类
* 1.构造方法私有
* 2.成员方法静态
* */
private Utils(){
}
public static String dataToString(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(date);
return s;
}
}
2.字节流
按照数据流向分为:
- 输入流:读数据
- 输出流:写数据
按照数据类型分:
- 字节流(字节输入流,字节输出流)
- 字符流(字符输入流,字符输出流)
一般通过Window自带的记事本软件打开,可以读懂的内容,就是用字符流;
否则使用字节流,如果不知道。就是用字节流;
2.1字节流使用
字节输出流
/*void write(byte[] b)
将 b.length个字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len)
将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
void write(int b)
将指定的字节写入此文件输出流。
*/
FileOutputStream fos = new FileOutputStream("untitled\\1.txt");//当前目录下
byte[] bys = {97,98,99};
byte[] bys1 = "abc".getBytes();
fos.write(bys);//abc
fos.write(bys1);//abc
fos.close();
字节输入流
/*int read()
从该输入流读取一个字节的数据。
int read(byte[] b)
从该输入流读取最多 b.length个字节的数据为字节数组。
int read(byte[] b, int off, int len)
从该输入流读取最多 len字节的数据为字节数组。
*/
FileInputStream fis = new FileInputStream("untitled\\1.txt");
int read = fis.read();
System.out.println(read);
System.out.println((char)read);
//创建输入流容器
byte[] by = new byte[1024];//一般1024及其整数倍
int len = fis.read(by);//len表示实际读取流的长度
System.out.println(new String(by));//转化成字符串
System.out.println(new String(by,0,len));//转化成有效字符串
fis.close();
高级使用
//输出流使用finally释放资源
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("output.txt");
fos.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制一个文件
public void test() throws IOException {
FileInputStream fis = new FileInputStream("E:\\web\\untitled2\\output.txt");
FileOutputStream fos = new FileOutputStream("outputcopy.txt");
//读写数据,一次读取一个字节数组,一次写入一个字节数组
byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){//文件读取完,为-1
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
3.字符流
由于字节流操作中文不是特别方便,所以提供了字符流
字符流 = 字节流 + 编码表
public void test01() throws IOException {
FileReader fr = new FileReader("E:\\web\\untitled2\\mn.txt");
FileWriter fw = new FileWriter("mnCopy.txt");
//读写数据,复制文件
int ch;
while((ch=fr.read())!=-1){
fw.write(ch);
}
//第二种方法
char[] chs = new char[1024];
int len;
while((len = fr.read(chs))!=-1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}
4.特殊操作流
4.1打印流
打印流分类:
- 字节打印流:PrintStream
- 字符打印流:PrintWriter
字节打印流PrintStream使用继承父类的方法写数据,查看时候会转码;使用自己的特有方法写数据,查看的数据原样输出;
PrintStream ps = new PrintStream("");
ps.write(97);//a ,父类方法
ps.println(97);//97,特有方法
ps.close();
PrintWrite pw = new PrintWrite(""); pw.write("hello"); pw.println("hello"); pw.flush(); pw.close();
4.1对象系列化
就是将对象保存在磁盘中,或者再网络中传输对象;
这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象中存储的属性等信息;
字节序列写到文件之后,相当于文件中持久保存了一个对象的信息;
反之,该字节还可以从文件中读取回来,重构对象,对他进行反序列化;
要实现序列化和反序列化就要使用对象序列化流和对象反序列化流:
- 对象序列化流:ObjectOutputStream
- 对象反序列化流:ObjectInputStream
4.1.1对象序列化流
ObjectOutputStream将Java对象的原始数据类型和图形写入OutputStream。可以使用ObjectInputStream读取(重构)对象。可以通过使用流的文件来实现对象的持久存储。如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象。
只有支持java.io.Serializable接口的对象才能写入流中。 每个可序列化对象的类被编码,包括类的类名和签名,对象的字段和数组的值以及从初始对象引用的任何其他对象的关闭。
方法writeObject用于将一个对象写入流中。 任何对象,包括字符串和数组,都是用writeObject编写的。 多个对象或原语可以写入流。 必须从对应的ObjectInputstream读取对象,其类型和写入次序相同。
FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close();
注意事项:
-
用对象序列化流序列化一个对象后,假如修改了对象所属的类文件,读取数据会出现问题
-
解决办法:给所属的类加一个值
private static final long serialVersionUID = 42L;
-
如果一个对象中的某个成员变量的值不想被序列化
private transient int age;//使用transient 关键字
4.1.2对象反序列化流
ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象。
ObjectOutputStream和ObjectInputStream可以分别为与FileOutputStream和FileInputStream一起使用的对象图提供持久性存储的应用程序。 ObjectInputStream用于恢复先前序列化的对象。 其他用途包括使用套接字流在主机之间传递对象,或者在远程通信系统中进行封送和解组参数和参数。
只能从流中读取支持java.io.Serializable或java.io.Externalizable接口的对象。
**方法readObject
**用于从流中读取对象。 应使用Java的安全铸造来获得所需的类型。 在Java中,字符串和数组是对象,在序列化过程中被视为对象。 读取时,需要将其转换为预期类型。
FileInputStream fis = new FileInputStream("t.tmp"); ObjectInputStream ois = new ObjectInputStream(fis); int i = ois.readInt(); String today = (String) ois.readObject(); Date date = (Date) ois.readObject(); ois.close();
4.2Properties类
-
作为Map集合使用;
-
Properties
可以保存到流中或从流中加载;//getProperty(String key) //使用此属性列表中指定的键搜索属性。//setProperty(String key, String value) //致电 Hashtable方法 put 。//load(InputStream inStream) //从输入字节流读取属性列表(键和元素对)。//store(Writer writer, String comments) //将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。