data[] pdata = (data[])in.readObject();
这行不对啊对象序列化怎么可能会序列出一个数组呢???/*改正后*/package com.oss.checkaccount;import java.io.Serializable;
public class data implements Serializable{ public String str="nihaoma";
public String str2="yumen,ceshi";
public data() {
}
}
package com.oss.checkaccount;import java.util.*;
import java.io.*;public class test {
public test() {
} private String Datechange() {
Calendar temp = Calendar.getInstance();
String strtmp = ".\\log\\";
strtmp += temp.get(Calendar.YEAR);
strtmp += (temp.get(Calendar.MONTH) + 1);
strtmp += temp.get(Calendar.DAY_OF_MONTH);
strtmp += ".txt";
return strtmp;
} public boolean WriteObjectToFile() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(Datechange(), true));
data pdate = new data();
out.writeObject(pdate);
out.close();
}
catch (InvalidClassException ic) {
System.out.println("InvalidClassException");
ic.getStackTrace();
}
catch (NotSerializableException ns) {
System.out.println("NotSerializableException");
ns.getStackTrace();
}
catch (IOException ioEx) {
ioEx.getStackTrace();
}
return true;
} public boolean ReadObjectToFile() {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(
new FileInputStream(Datechange()));
data pdata = (data) in.readObject();
System.out.println(pdata.str);
System.out.println(pdata.str2);
in.close();
}
catch (InvalidClassException ic) {
System.out.println("InvalidClassException");
ic.getStackTrace();
}
catch (ClassNotFoundException ns) {
System.out.println("ClassNotFoundException");
ns.getStackTrace();
}
catch (StreamCorruptedException sce) {
System.out.println("StreamCorruptedException");
sce.getStackTrace();
}
catch (OptionalDataException ode) {
System.out.println("StreamCorruptedException");
ode.getStackTrace();
}
catch (IOException ioEx) {
System.out.println(ioEx.getMessage());
ioEx.printStackTrace();
try {
in.close();
}
catch (Exception ex) {
System.out.println("close error!");
}
} return true;
} public static void main(String args[]) {
test ptest = new test();
ptest.WriteObjectToFile();
ptest.ReadObjectToFile();
}
}
这行不对啊对象序列化怎么可能会序列出一个数组呢???/*改正后*/package com.oss.checkaccount;import java.io.Serializable;
public class data implements Serializable{ public String str="nihaoma";
public String str2="yumen,ceshi";
public data() {
}
}
package com.oss.checkaccount;import java.util.*;
import java.io.*;public class test {
public test() {
} private String Datechange() {
Calendar temp = Calendar.getInstance();
String strtmp = ".\\log\\";
strtmp += temp.get(Calendar.YEAR);
strtmp += (temp.get(Calendar.MONTH) + 1);
strtmp += temp.get(Calendar.DAY_OF_MONTH);
strtmp += ".txt";
return strtmp;
} public boolean WriteObjectToFile() {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(Datechange(), true));
data pdate = new data();
out.writeObject(pdate);
out.close();
}
catch (InvalidClassException ic) {
System.out.println("InvalidClassException");
ic.getStackTrace();
}
catch (NotSerializableException ns) {
System.out.println("NotSerializableException");
ns.getStackTrace();
}
catch (IOException ioEx) {
ioEx.getStackTrace();
}
return true;
} public boolean ReadObjectToFile() {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(
new FileInputStream(Datechange()));
data pdata = (data) in.readObject();
System.out.println(pdata.str);
System.out.println(pdata.str2);
in.close();
}
catch (InvalidClassException ic) {
System.out.println("InvalidClassException");
ic.getStackTrace();
}
catch (ClassNotFoundException ns) {
System.out.println("ClassNotFoundException");
ns.getStackTrace();
}
catch (StreamCorruptedException sce) {
System.out.println("StreamCorruptedException");
sce.getStackTrace();
}
catch (OptionalDataException ode) {
System.out.println("StreamCorruptedException");
ode.getStackTrace();
}
catch (IOException ioEx) {
System.out.println(ioEx.getMessage());
ioEx.printStackTrace();
try {
in.close();
}
catch (Exception ex) {
System.out.println("close error!");
}
} return true;
} public static void main(String args[]) {
test ptest = new test();
ptest.WriteObjectToFile();
ptest.ReadObjectToFile();
}
}
解决方案 »
- this上面和下面有什么区别吗?
- 一个简单问题
- 在线帮小弟一个忙,如何用java实现开发工具中的智能提示
- 不知道这个程序哪错了.
- 如何将字符串转换成字符串数组?
- 调查,你最喜欢什么java开发工具?jbuilder,intellij idea,jcreator,netbeans,eclipse还是其他的,最好写下喜欢的理由,大家支持下可
- j2se j2ee j2me到底什么区别?
- 序列化(serialization)时出错!!!!
- Applet如何打开一个新的IE窗口?
- 哪位大哥为我介绍一下“多线程”,我看了书,可还是不太明白,谢谢
- 在linux的mysql有中文问题,怎办?
- 大家是在用1.4还是在用1.5(5.0)
对了,zncn2(Zn(CN)2) 兄,如果存储了n个对象是不是我只能一个一个的读出来:例如可以这样写伪代码:
List plist = new ArrayList();
while(!feof(fp)){
plist.add((data) in.readObject());
}
而不能data[] pdata = (data[])in.readObject();
我看网上有个例子是这样写的。
有没有更好的办法呢,讨论一下吧,下班前就揭帖。。
FileInputStream filein = new FileInputStream(Datechange());
ObjectInputStream in = new ObjectInputStream(filein);
List plist = new ArrayList();
while(filein.available() != 0){
plist.add(in.readObject());
/*跳过4字节的版本信息*/
if(filein.available()!=0)
filein.skip(4);
}
for(int i=0;i<plist.size();++i){
data pdata = (data)plist.get(i);
System.out.println(pdata.str);
System.out.println(pdata.str2);
}
in.close();
FileInputStream filein = new FileInputStream(Datechange());
ObjectInputStream in = new ObjectInputStream(filein);
List plist = new ArrayList();
while(filein.available() != 0){
plist.add(in.readObject());
/*跳过4字节的版本信息*/
if(filein.available()!=0)
filein.skip(4);
}
for(int i=0;i<plist.size();++i){
data pdata = (data)plist.get(i);
System.out.println(pdata.str);
System.out.println(pdata.str2);
}
in.close();
while(filein.available() != 0){
plist.add(in.readObject());
if(filein.available()!=0){
if(filein.read() == 0xac && filein.read() == 0xed)
filein.skip(2);
else
filein.skip(-1); //回跳
}
}