前言:十一过后这漫长的七天工作日,因为要配合北京测试的同事改bug,让我又多了一个在公司通宵加班的日子。可怜的博客有点被我懈怠了,到了周末我们继续对java的复习。这篇博文是Java IO流系列的补充,共三个部分。第一,补充介绍打印流PrintStream和PrintWriter的基本用法。第二部分,补充介绍序列流SequenceInputStream的基本用法。最后一个部分补充介绍ObjectInputStream和ObjectOutputStream。
原文出处:http://blog.csdn.net/u014158743/article/details/52831297
PrintStream字节打印流
import java.io.*;
class Demo {
public static void main(String[] args)throws IOException {
/*
PrintStream:字节输出流,具备了字节输出流的基本功能,增加了打印功能
可以操作的目的:1:File类型的文件
2:字符串类型的文件
3:字节输出流
*/
PrintStream ps = new PrintStream("tt.txt");
//ps.write(97);// 写入的是一个字节 00000000 00000000 00000000 01100001-->砍掉前三个字节 97
ps.write(353);//00000000 00000000 00000001 01100001-->砍掉前三个字节 97
//ps.println(353);//按照数据的原样显示,也就是数据的表现形式,内部使用了 String.valueOf(),同下
ps.write(String.valueOf(353).getBytes());
}
}
PrintWriter字符打印流
import java.io.*;
class Demo {
public static void main(String[] args)throws IOException {
/*
PrintWriter:字符打印流,字符输出流的基本功能都具备了,增加了打印功能
可以操作的目的:
1:File类型的文件
2:字符串类型的文件
3:字节输出流
4:字符输出流
*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//PrintWriter pw = new PrintWriter(System.out,true);//自动刷新
PrintWriter pw = new PrintWriter(new FileWriter("t.txt"),true);
String line = null;
while((line = br.readLine())!=null)
{
if("over".equals(line))
break;
pw.println(line);
//pw.write(line);
//pw.write("\r\n");
//pw.flush();
}
}
}
SequenceInputStream序列流
合并文件的小Demo
class Demo {
public static void main(String[] args)throws IOException {
//序列流
/*
FileInputStream fis1 = new FileInputStream("part1.txt");
FileInputStream fis2 = new FileInputStream("part2.txt");
FileInputStream fis3 = new FileInputStream("part3.txt");
Vector v = new Vector();
v.add(fis1);
v.add(fis2);
v.add(fis3);
Enumeration<FileInputStream> en = v.elements();*/
List<FileInputStream> list = new ArrayList<FileInputStream>();
for(int i=11;i<=13;i++) {
// 同目录下的三个用来合并的文件
list.add(new FileInputStream("part"+i+".txt"));
}
Enumeration<FileInputStream> en = Collections.enumeration(list);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("hebing.txt");
byte[] arr = new byte[1024];
int len = 0;
while((len = sis.read(arr))!=-1) {
fos.write(arr,0,len);
}
sis.close();
fos.close();
}
}
对象流ObjectInputStream和ObjectOutputStream
//序列化,Person实体类
class Person implements Serializable {
static final long serialVersionUID = 42L;//显示声明序列号
public transient String name;//瞬态 不参与序列化
private int age;
public Person(){}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
class Demo {
public static void main(String[] args)throws IOException,ClassNotFoundException {
writeObject();
readObject();
}
//反序列化:把存储的对象从设备中读出,对象是依赖于其所属的类的
public static void readObject()throws IOException,ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
Person person =(Person)ois.readObject();//1384489234623001664
System.out.println(person.getName()+","+person.getAge());
}
//序列化:把对象进行持久存储
public static void writeObject()throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
oos.writeObject(new Person("李四",23));//-2747677651716560467
oos.close();
}
}