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();
}
}
解决方案 »
- jdbc中的事物回滚后要不要再提交一下?
- 求javase版的植物大战僵尸联机版源码一份
- bat中执行java
- Java动态生成导入类(高手请进)
- 关于线程使用的一个小问题???
- 如何将两个字节数组合并成一个字节数组
- JBuilder9 enterprise enition download here ! come in!
- 因工作需要?哪里还有JDK1.1.1版本下载啊?
- 这个问题这么解啊???欢迎讨论!!!高分。。。。
- 奇怪,通过 JAVA xmlbean jar 开发JAVA操作XML文件的问题,获取value值的问题
- 在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); //回跳
}
}