IO流(2)

来源:互联网 发布:ios网络电视 编辑:程序博客网 时间:2024/06/05 09:05
④ BufferedOutputStream/BufferedInputStream

File f=new File("D://8.txt"); //已经被写入“我有一只小毛驴”
InputStream is=new FileInputStream(f);
BufferedInputStream br=new BufferedInputStream(is);
Reader r=new InputStreamReader(br);
char ch[]=new char[1024];
int len=-1;
StringBuffer sb=new StringBuffer();
while((len=r.read(ch))!=-1){
sb.append(new String(ch,0,50));
}
is.close();
r.close();
br.close();
System.out.println(sb);
}
public static void main(String[] args) throws IOException {
InputStreamReader2();
}

输出结果:我有一只小毛驴
再写一个缓冲转换输出流

public static void Out() throws IOException{
File f2=new File("D://9.txt");
OutputStream os=new FileOutputStream(f2,true);
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
String s3="我希望w自己x能赶t上移动互联u网时代";
bw.write(s3);
bw.close();
osw.close();
os.close();
}
public static void main(String[] args) throws IOException {
Out();
}

⑤PrintStream/PrintWriter

public static void print() throws IOException{
OutputStream os=new FileOutputStream("D://op.txt");
BufferedOutputStream bos=new BufferedOutputStream(os);
PrintStream ps=new PrintStream(bos);
ps.print(9.098);
ps.print(true);
ps.print("我呵呵");
ps.print(87);
ps.close();
bos.close();
os.close();
}

public static void main(String[] args) throws IOException {
print();
}

PrintStream和PrintWriter差别不大,后者实现了前者的所有功能,不同的是,后者的自动刷新需要println等方法,否则不会自动刷新。
⑥ ObjectOutputStream/ObjectInputStream
对象需要实现Serializable接口,让后被转换成byte字节流。

public static void Output(){
try {
OutputStream os=new FileOutputStream("D://yui.tmp");
ObjectOutputStream oos=new ObjectOutputStream(os);
Dog d1=new Dog("小白",7);
Dog d2=new Dog("小黑",5);
oos.writeObject(d1);
oos.writeBoolean(true);
oos.writeObject(d2);
oos.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Output();
}

要记住对象写入的顺序,因为读取的时候要按照顺序来,否则会乱码。

public static void input() throws IOException, ClassNotFoundException{
InputStream is=new FileInputStream("D://yui.tmp");
ObjectInputStream ois=new ObjectInputStream(is);
Object obj=ois.readObject();
Dog dog1=(Dog)obj;
Boolean bl=ois.readBoolean();
Dog dog2=(Dog)ois.readObject();
System.out.println(bl);
System.out.println(dog1);
System.out.println(dog2);
ois.close();
is.close();
}
public static void main(String[] args) {
try {
input();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

反序列化的时候,顺序要按照写入的顺序来。而且,注意可能会出现类型不匹配。
⑦  ByteArrayInputStream/ByteArrayOutputSTream
就像是一个包装带,把信息打包成一个字节数组,方便网络传递。具体什么特点我也不清楚。关闭无效,自带缓冲区。
用法不详细列出。
⑧DataInputStream/DataOutputStream
允许程序用与机器无关的底层输入流中读取Java基本类型。如果要读取的话也要按照写入的顺序读。

public static void Dataout(){
try {
OutputStream os=new FileOutputStream("D://io.tmp");
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(45);
dos.writeUTF("按顺序读取");
dos.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Dataout();
}


读取操作和对象流类似,这里就不列出来了。
0 0
原创粉丝点击