频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

Java语言深入 文件和流(3)

作者: 出处:中国IT实验室  (  ) 砖  (  ) 好  评论 ( ) 条  进入论坛
更新时间:2006-11-17 10:23
关 键 词:Java  文件  
阅读提示:Java I/O系统的类实在是太多了,这里我们只学习一些基本的和常用的,相信能够掌握这些就可以解决我们以后的普通应用了。

8.对象流

串行化:对象通过写出描述自己状态的数值来记述自己的过程叫串行话。
对象流:能够输入输出对象的流。

将串行化的对象通过对象流写入文件或传送到其他地方。
对象流是在普通流上加了传输对象的功能,所以构造对象流时要先构造普通文件流。

注意:只有实现了Serializable接口的类才能被串行化

例子:

import java.io.*;
class Student implements Serializable{
private String name;
private int age;

public Student(String name,int age){
this.name=name;
this.age=age;
}

public void greeting(){
System.out.println("hello ,my name is "+name);
}

public String toString(){
return "Student["+name+","+age+"]";
}
}
public class ObjectOutTest{
public static void main(String args[]){
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(
new FileOutputStream("student.txt"));
Student s1=new Student("Jerry",24);
Student s2=new Student("Andy",33);

oos.writeObject(s1);
oos.writeObject(s2);
}catch(Exception e){
e.printStackTrace();
}finally{
if(oos!=null)
try{
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}

import java.io.*;
public class ObjectInTest{
public static void main(String args[]){
ObjectInputStream ois=null;
Student s=null;
try{
ois=new ObjectInputStream(
new FileInputStream("student.txt"));
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
}catch(Exception e){
e.printStackTrace();
}finally{
if(ois!=null)
try{
ois.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}

9.字符流InputStreamReader/OutputStreamWriter

上面的几种流的单位是byte,所以叫做字节流,写入文件的都是二进制字节,我们无法直接看,下面要学习的是字节流。

Java采用Unicode字符集,每个字符和汉字都采用2个字节进行编码,ASCII码是Unicode编码的自集InputStreamReader是字节流到字符桥的桥梁(byte->char读取字节然后用特定字符集编码成字符)。

OutputStreamWriter是字符流到字节流的桥梁(char->byte)。

他们是在字节流的基础上加了桥梁作用,所以构造他们时要先构造普通文件流。

我们常用的是:

BufferedReader方法:readLine()
PrintWriter方法:println()

例子:

import java.io.*;
public class PrintWriterTest{
public static void main(String args[]){
PrintWriter pw=null;
try{
pw=new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("bufferedwriter.txt")));
pw.println("hello world");
}catch(Exception e){
e.printStackTrace();
}finally{
if(pw!=null)
try{
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}

import java.io.*;
public class BufferedReaderTest{
public static void main(String args[]){
BufferedReader br=null;
try{
br=new BufferedReader(
new InputStreamReader(
new FileInputStream("bufferedwriter.txt")));
System.out.println(br.readLine());
}catch(Exception e){
e.printStackTrace();
}finally{
if(br!=null)
try{
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}


共4页: 上一页 [1] [2] 3 [4] 下一页
【内容导航】
发表
查看
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看52371次)
·UML类图详解 (查看46839次)
·Java编程开发手册 (查看25118次)
·UML统一建模语言 (查看24099次)
·C#技术开发指南 (查看22406次)
·Java编程开发手册 (1195个砖)
·Java基础教程 (429个砖)
·C#技术开发指南 (304个砖)
·PB开发教程 (220个砖)
·.NET开发手册 (217个砖)
·Java编程开发手册 (653个好)
·Java基础教程 (569个好)
·.NET开发手册 (251个好)
·PB开发教程 (209个好)
·Delphi开发技术手册 (174个好)
订阅技术快讯
电子杂志下载
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。
名称:Vista精品应用黄皮书
简介:《Vista精品应用黄皮书》囊括了Vista的各方面内容。此次的精简版,是将里面的内容做了提取,便于用户下载和使用。内容包含了各种Vista的安装与实施、技巧与解析以及各种Vista相关学习文档和相关软件的安全下载。该电子书是了解和应用Vista人员必备的工具手册,并且也是第一本
名称:2006中国IT论坛精品集合
简介:本书由“51CTO论坛推广联盟”制作完成。书中所有内容均来自各联盟成员的论坛(网站)。制作本书的目的是为了集中大家的优势资源,将更多更精彩的内容带给广大技术爱好者。本书是联盟成立以来制作的第一本书。
关键字阅读
频道精选
主编信箱 热线:010-66476606 告诉我们您想看的:专题 文章
关于我们 | 诚聘英才 | 联系我们 | 网站大事 | 意见反馈 | 网站地图
Copyright©2005-2007 51CTO.COM 版权所有