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]
# 1
Was the file "logInfo.log" written by Java using ObjectOutputStream?If so, were AuditRecordImpl objects written into the file?
ChuckBinga at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 2
yes correct, i written into a file (logInfo.log).how can i print the attribute in console window
MCA134a at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 3

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;

}

}

jawadhashmia at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 4
Sorry jawadhashmi sir,it throws same Exceptionjava.io.StreamCorruptedException: invalid stream header what i do? help me!!
MCA134a at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 5
Dear I think you are storing/writting some other object and reading some one else.My example is working fine I tested and then posted.check only the example and then compare it with your code.
jawadhashmia at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 6
yes jawadhashmi sir,your code working properly. i think this my mistake.ok sir bye.Thank you for replaying
MCA134a at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 7
>oinStream.available() > 0this might stop you while reading more then one stored objects. you can change it to true just for time. Then you can check for EOF.
jawadhashmia at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 8
where are my dukes :)
jawadhashmia at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 9
Cross post http://forum.java.sun.com/thread.jspa?threadID=5162515&messageID=9620173#9620173
Satish_Patila at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...
# 10
Satish_Patil sir,that question i posted in morning, but i got reply before 30min
MCA134a at 2007-7-12 0:40:22 > top of Java-index,Java Essentials,Java Programming...