[关闭]
@XQF 2017-02-16T17:57:12.000000Z 字数 1786 阅读 1394

工作空间的相对路径和绝对路径

java


  1. 相对路径:src/com/leetcode/xqf/hah.txt
  2. 绝对路径就要把盘符加上

参考博文

1.不规范的序列化

  1. class Student implements Serializable {
  2. private String name;
  3. private int age;
  4. public Student(String name, int age) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. public String toString() {
  9. return "name :" + name + " " + "age :" + age;
  10. }
  11. }
  12. public class Test {
  13. public static void main(String[] args) {
  14. File file = new File("src/com/leetcode/xqf/hah.txt");
  15. FileInputStream fileIn = null;
  16. try {
  17. fileIn = new FileInputStream(file);
  18. FileOutputStream fileOut = new FileOutputStream(file);
  19. ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
  20. objectOut.writeObject(new Student("XQF", 23));
  21. objectOut.writeObject(new Student("WLY", 20));
  22. ObjectInputStream objectIn = new ObjectInputStream(fileIn);
  23. Student s1 = (Student) objectIn.readObject();
  24. System.out.println(s1);
  25. Student s2 = (Student) objectIn.readObject();
  26. System.out.println(s2);
  27. } catch (FileNotFoundException e) {
  28. e.printStackTrace();
  29. } catch (ClassNotFoundException e) {
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

2.规范的序列化(UUID)

  1. ublic class Serializable01 extends Object implements Serializable{
  2. private static final long serialVersionUID = -1466479389299512377L;
  3. public byte b = 12;
  4. public byte getB() {
  5. return b;
  6. }
  7. public void setB(byte b) {
  8. this.b = b;
  9. }
  10. public static void main(String[] args) {
  11. try {
  12. FileOutputStream fos = new FileOutputStream(new File("E:\\tmp\\demo2.txt"));
  13. ObjectOutputStream os = new ObjectOutputStream(fos);
  14. Serializable01 s = new Serializable01();
  15. os.writeObject(s);
  16. os.flush();
  17. os.close();
  18. FileInputStream fis = new FileInputStream(new File("E:\\tmp\\demo2.txt"));
  19. ObjectInputStream ois = new ObjectInputStream(fis);
  20. Serializable01 s2 = (Serializable01)ois.readObject();
  21. System.out.println(s2.getB());
  22. ois.close();
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. } catch (ClassNotFoundException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注