/**CSDN Question:
 * 最近学到IO遇到了几个问题 
 * 1)IO读取将文件另存为Unicode编码的txt文件会出现乱码?//Google ok
 * 2)如何删除带子目录的目录 ?//Google ok
 * 
 * 3)ObjectInputStream 中static变量仍可能修改和存储?
 *   可老师说不行是不是和JDK的版本有关我用的是1.6的
 *   
 * 4)// TODO Auto-generated method stub这句是Eclipse自动生成的
 *   请问如何自定义让Eclipse不自动添加部分注释或取消所有自动生成的注释 //Google ok 
 * */
public class MyQuestion {
public static void main(String[] args) {
QuestionOne();
QuestionTwo();
QuestionThree();

}
private static void QuestionOne() {
}

private static void QuestionTwo() {
} private static void QuestionThree() {
TestObjectStream.ObjectWrite();
TestObjectStream.ObjectRead();
}


}
/**实现序列化使用对象可以写入流中
 * */
class Person implements Serializable{
private String name;
public static int age = 1;
private transient String caption; public Person(){}
public Person(String name, int age, String caption) {
super();
this.name = name;
this.age = age;
this.caption = caption;
}

}/**将Person对象写入object.txt
 * 从object.txt中读取Person对象
 * */
class TestObjectStream {
public static void ObjectWrite(){
File file = new File("E:\\object.txt");
FileOutputStream write = null;
ObjectOutputStream objectWrite = null;
try{
write = new FileOutputStream(file);
objectWrite = new ObjectOutputStream(write);

//创建Person对象设置静态变量
Person p = new Person();
Person.age = 111;
//将对象写入文件
objectWrite.writeObject(p);
objectWrite.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
objectWrite.close();
write.close();
System.out.println("写入对象成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void ObjectRead(){
File file = new File("E:/object.txt");
FileInputStream read = null;
ObjectInputStream objectRead = null;
try{
read = new FileInputStream(file);
objectRead = new ObjectInputStream(read);

//读取Person对象获得年龄
//看老师的视频age得到的是1
//而这里我得到的是修改后的111
Person tempPerson = (Person) objectRead.readObject();
System.out.println(tempPerson.age);
}catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
objectRead.close();
read.close();
System.out.println("读取对象成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}第三个问题不明白
其它已解决

解决方案 »

  1.   

    我把一个对象写入文件中
    再读取对象数据都是说static 和transient 的属性会被存入
    可是我试验了 static的也会被存入 为什么?
      

  2.   

    没有人理CSDN应该定时把无人回的帖提前才是...
      

  3.   

    可能你没有搞懂老师的意思。
    类static变量在对象序列化输出时,并没有输出到输出流里面。你可以做这样的实验,你再读的代码之前设置一下age的值,然后再读取person对象,如果age(111)被写入了
    到object.txt,那么读取后Person.age==111。实例代码如下:read = new FileInputStream(file);
                objectRead = new ObjectInputStream(read);
                
                //读取Person对象获得年龄
                //看老师的视频age得到的是1
                //而这里我得到的是修改后的111
                Person.age = 9527;//
                Person tempPerson = (Person) objectRead.readObject();
                System.out.println(tempPerson.age);//还是输出9527,证明readObject()没有改变age值
      

  4.   


    package com;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;public class TestObjectReader {
    public static void main(String[] args) {
    ObjectOutputStream objectOutputStream = null;
    try {
    objectOutputStream = new ObjectOutputStream(new FileOutputStream("e:/object.txt"));
    Person person = new Person();
    Person.age = 222;
    objectOutputStream.writeObject(person);
    objectOutputStream.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    if(objectOutputStream != null){
    try {
    objectOutputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    ObjectInputStream objectInputStream = null;
    try {
    objectInputStream  = new ObjectInputStream(new FileInputStream("e:/object.txt"));
    Person.age = 444;
    Person p = (Person) objectInputStream.readObject();
    System.out.println(p.age);

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }finally{
    if(objectInputStream != null){
    try {
    objectInputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }



    }
    }class Person implements Serializable{
    public static int age = 111;
    }这段代码结果p.age = 444; 说明原来没有把222写到文件中 
    而Person.age=444 可以p.age使用的是Person.age=444的值.