IO流部分实例讲解
2012-12-30 21:49
218 查看
IO流部分:能够进行文件的简单读写操作,能够将文件的内容读取出来;会使用过滤流如BufferedInputStream的使用;了解装饰设计模式
BufferedReaderTest.java
person.java
BufferedReaderTest.java
package jsj.java.exam.test06; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderTest { public static void main(String[] args) { BufferedReader reader=null; //创建缓冲输入流,接收从键盘中输入一行数据并进行输出显示 //要进行相应的异常处理 //注意关闭流 //reader=________061________ reader=new BufferedReader(new InputStreamReader(System.in));//2 System.out.println("请输入一行数据"); try { String str=reader.readLine();//1 System.out.println("你输入的数据为:"+str); } catch (IOException e) {//1 // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }ObjectInputStreamDemo .java
package jsj.java.exam.test06; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { /** * @param args * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { File file=new File("d:"+File.separator+"person.txt"); ObjectInputStream ois=null; ois=new ObjectInputStream(new FileInputStream(file)); //Object obj=________064________;//将对象恢复出来 Object obj=ois.readObject();//2 Person person=(Person)obj; System.out.println(person); } }ObjectOutputStreamDemo.java
package jsj.java.exam.test06; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class ObjectOutputStreamDemo { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { // TODO Auto-generated method stub File file=new File("d:"+File.separator+"person.txt"); ObjectOutputStream oos=null; //创建对象输出流 //oos=________063________;//2 oos=new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(new Person("张三", 30)); oos.close(); } }
person.java
package jsj.java.exam.test06; import java.io.Serializable; public class Person implements Serializable { private String name; //定义年龄属性,此属性不需要持久化保存 //________062________//2 private transient int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } }
相关文章推荐
- 在NetBeans平台上开发J2ME游戏实例讲解(第三部分)
- 在NetBeans平台上开发J2ME游戏实例讲解(第二部分)
- 在NetBeans平台上开发J2ME游戏实例讲解(第二部分)
- [零基础学JAVA]Java SE应用部分-31.Java IO操作(05)IO操作实例讲解之实现简单MIS 推荐
- 用NetBeans开发平台开发J2ME游戏实例讲解(第四部分)
- 在NetBeans平台上开发J2ME游戏实例讲解(第三部分)
- tensorflow 加载部分变量的实例讲解
- ajax讲解和入门实例
- Hadoop 实例9 Join讲解2: 将人员的地址ID完善成为地址名称
- linux下C语言多线程编程实例及讲解
- 第六部分 PHP实例
- LockSupport简单讲解及实例
- Oracle start with..Connect By prior..用法实例讲解
- MySQL 实例讲解联合查询
- JSON.stringify 语法实例讲解
- Java JUC之Atomic系列12大类实例讲解和原理分解
- 基于vue.js路由参数的实例讲解――简单易懂
- 在python3.5中使用OpenCV的实例讲解
- 思科交换机MST配置命令步骤-实例讲解
- Yii2使用驼峰命名的形式访问控制器(实例讲解)