Chinaunix首页 | 论坛 | 博客
  • 博客访问: 711119
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: Java

2011-10-27 21:12:47

  1. package cn.youhap.headfirst.H15;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. import java.io.ObjectOutputStream;
  15. import java.util.ArrayList;

  16. public class ReadAndWriteFile {

  17.     public static void main(String[] args) {
  18.         ReadAndWriteFile readAndWriteFile = new ReadAndWriteFile();
  19.         try {
  20.             readAndWriteFile.readAndWriteTextFile();
  21.             readAndWriteFile.readAndWriteBinFile();
  22.             readAndWriteFile.readAndWriteObjectFile();
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         } catch (ClassNotFoundException e) {
  26.             e.printStackTrace();
  27.         }
  28.     }

  29.     // 读写普通的txt文件
  30.     public void readAndWriteTextFile() throws IOException {
  31.         File file = new File("C:\\testDir\\p.txt");
  32.         BufferedReader br = null;
  33.         BufferedWriter bw = null;
  34.         // read
  35.         try {
  36.             FileReader reader = new FileReader(file);
  37.             br = new BufferedReader(reader);

  38.             FileWriter writer = new FileWriter(new File("C:\\testDir\\p2.txt"));
  39.             bw = new BufferedWriter(writer);

  40.             String line = null;
  41.             StringBuffer sb = new StringBuffer();
  42.             while ((line = br.readLine()) != null) {
  43.                 sb.append(line + "\r\n");
  44.             }
  45.             bw.write(sb.toString());
  46.         } catch (FileNotFoundException e) {
  47.             e.printStackTrace();
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         } finally {
  51.             bw.flush();
  52.             br.close();
  53.             bw.close();
  54.         }
  55.     }

  56.     // 读写二进制文件
  57.     public void readAndWriteBinFile() throws IOException {
  58.         // read a jpeg file
  59.         File file = new File("C:\\testDir\\qq.PNG");
  60.         FileInputStream fis;
  61.         BufferedInputStream bis = null;

  62.         FileOutputStream fos;
  63.         BufferedOutputStream bos = null;
  64.         try {
  65.             fis = new FileInputStream(file);
  66.             bis = new BufferedInputStream(fis);

  67.             fos = new FileOutputStream(new File("C:\\testDir\\qq2.PNG"));
  68.             bos = new BufferedOutputStream(fos);
  69.             byte[] b = new byte[1024];
  70.             int counter = 0;
  71.             while ((counter = bis.read(b, 0, b.length)) != -1) {
  72.                 fos.write(b, 0, counter);
  73.             }
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         } finally {
  79.             bos.flush();
  80.             bis.close();
  81.             bos.close();
  82.         }
  83.     }

  84.     // 读写序列化对象
  85.     public void readAndWriteObjectFile() throws IOException,
  86.             ClassNotFoundException {
  87.         // write object
  88.         File file = new File("C:\\testDir\\person.ser");
  89.         FileOutputStream fos = new FileOutputStream(file);
  90.         ObjectOutputStream oos = new ObjectOutputStream(fos);

  91.         Person p1 = new Person();
  92.         p1.setName("bbb");
  93.         p1.setAge(33);
  94.         p1.setHeight(172);

  95.         Person p2 = new Person();
  96.         p2.setName("aaaa");
  97.         p2.setAge(30);
  98.         p2.setHeight(160);

  99.         ArrayList<Person> pList = new ArrayList<Person>();
  100.         pList.add(p1);
  101.         pList.add(p2);

  102.         oos.writeObject(pList);
  103.         oos.flush();

  104.         // reade object list
  105.         FileInputStream fis = new FileInputStream(file);
  106.         ObjectInputStream ois = new ObjectInputStream(fis);
  107.         pList = ((ArrayList<Person>) ois.readObject());
  108.         StringBuffer sb = new StringBuffer();
  109.         for (Person p : pList) {
  110.             sb.append("name: " + p.getName() + ", ");
  111.             sb.append("age: " + p.getAge() + ", ");
  112.             sb.append("height: " + p.getHeight());
  113.             sb.append("\r\n");
  114.         }

  115.         System.out.println(sb);
  116.     }
  117. }

  1. package cn.youhap.headfirst.H15;

  2. import java.io.Serializable;

  3. public class Person implements Serializable {
  4.     private static final long serialVersionUID = -6907689374622606005L;
  5.     private int age;
  6.     private transient int Height;
  7.     private String Name ;
  8.     public int getAge() {
  9.         return age;
  10.     }
  11.     public int getHeight() {
  12.         return Height;
  13.     }
  14.     public String getName() {
  15.         return Name;
  16.     }
  17.     public void setAge(int age) {
  18.         this.age = age;
  19.     }
  20.     public void setHeight(int height) {
  21.         Height = height;
  22.     }
  23.     public void setName(String name) {
  24.         Name = name;
  25.     }
  26. }

阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~