Java File Concept
hi guys,
have some problem in console window, i want to print (AuditRecord object) attribute and this file(logInfo.log) have contain some object that is AuditRecord information and my code is
FileInputStream file = new FileInputStream(new File("logInfo.log"));
ObjectInputStream dataFile = new ObjectInputStream(file);
AuditRecord p = new AuditRecordImpl(); //it is object and it contain some values like name, attributes, etc
while((p=(AuditRecordImpl)dataFile.readObject())!=null)
{
System.out.println("The Audit Record : " + p.getAttributes()); // i want to print attribute
}
file.close();
dataFile.close();
if i tried this line the output displayed like
java.io.StreamCorruptedException: invalid stream header: 31302E31
what i do ? help me guys!
[835 byte] By [
MCA134a] at [2007-11-27 1:33:48]

check this out.
import java.io.*;
public class WriteObjectToFile {
public static void main(String [] arg) {
WriteObjectToFile wotFile = new WriteObjectToFile();
wotFile.WriteToFile();
wotFile.ReadFromFile();
}
private boolean WriteToFile(){
try{
File file = new File("abc.txt");
FileOutputStream foutStream = new FileOutputStream(file);
ObjectOutputStream ooutStream = new ObjectOutputStream(foutStream);
UserInfo uInfo = new UserInfo(1, "some name");
ooutStream.writeObject(uInfo);
ooutStream.flush();
}catch(Exception e){
e.printStackTrace();
}
return true;
}
private boolean ReadFromFile(){
try{
File file = new File("abc.txt");
FileInputStream finStream = new FileInputStream(file);
ObjectInputStream oinStream = new ObjectInputStream(finStream);
UserInfo uInfo = null;
do{
uInfo = (UserInfo)oinStream.readObject();
System.out.println(uInfo.getId());
}while(oinStream.available() > 0);
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
class UserInfo implements Serializable{
private String strName;
private int id;
public UserInfo(int id, String name) {
this.strName = name;
this.id = id;
}
public String getName(){
return this.strName;
}
public int getId(){
return this.id;
}
}