1. package org.jr.io;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2004年8月31日
  6. * @author Cherami
  7. */
  8. import java.io.*;
  9. /**
  10. * 此类中封装一些常用的写文件操作。
  11. * 所有方法都是静态方法,不需要生成此类的实例,
  12. * 为避免生成此类的实例,构造方法被申明为private类型的。
  13. * @since 0.6
  14. */
  15. public class FileWriter {
  16. /**
  17. * 私有构造方法,防止类的实例化,因为工具类不需要实例化。
  18. */
  19. private FileWriter() {
  20. }
  21. /**
  22. * 从指定的文件中读取一个序列化的对象。
  23. * @param filename 文件名
  24. * @return 序列化文件对应的对象,有任何错误返回null。
  25. * @since 0.6
  26. */
  27. public static void writeObject(String filename,Object object) {
  28. ObjectOutputStream out=null;
  29. try {
  30. out=new ObjectOutputStream(new FileOutputStream(filename));
  31. out.writeObject(object);
  32. out.close();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. } finally {
  36. if (out!=null) {
  37. try {
  38. out.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }
  45. }