简体   繁体   中英

Not able to read object and data from the file at same time in java

Not able to read object and data from the file at the same time in java m able to write the object into the file but not able to fetch all the objects only first object is fetched and also data after object is not able to retrieve

 import java.io.*;
 import java.util.*;

 class writeobj implements Serializable
 {
   public String name;
   public long size;
 }

 class FileLists
 {
   public static void main(String[] args)  throws Exception
   {
   try{
    File folder = new File("/home/shubham/Desktop/packer/dem");

    File[] files = folder.listFiles();

    FileOutputStream fobj = new FileOutputStream("myfile.ser");

     ObjectOutputStream oobj = new ObjectOutputStream(fobj);
      int ch;

    for (File file : files) 
        {   
            if (file.isFile())
                {  
            writeobj obj = new writeobj();
                    obj.name = file.getName();
            obj.size = file.length();
            oobj.writeObject(obj);
            String str = file.getAbsolutePath();
            FileInputStream fre =new FileInputStream(str); 
            System.out.println(file.getName()+"-"+file.length()+"-"+str);


            //FileReader f = new FileReader(obj.name);
            byte[] buffer = new byte[1024];

            while((ch = fre.read(buffer))!=-1){
                //System.out.println((char)ch);
                fobj.write(buffer,0,ch);
            }
            //Fread = null;
            fre.close();
            obj = null;
        }
        }
}
catch(Exception e)
{
    System.out.println(e);
}       
}
}

reading from this file it only read first object and create that file but after that not data not object are able to read from the myfile.ser

import java.io.*; 
import java.util.*;

  class writeobj implements Serializable
  {
    public String name;
    public long size;
   }
  class FileLists
  {
   public static void main(String[] args) 
{

int ch;
//File folder = new File("/home/shubham/Desktop/packer/dem/hello/demo");
try
{
    FileInputStream fobj = new FileInputStream("myfile.ser");
    //BufferedInputStream br = new BufferedInputStream(fobj);

    ObjectInputStream ois = new ObjectInputStream(fobj);
    writeobj e;
    while( (e = (writeobj)ois.readObject()) != null)
    {
        FileWriter f = new FileWriter(e.name);

        System.out.println(e.name+"name :"+e.size);

        while((ch=ois.read())!= -1){
            System.out.println("as");
        }
    }   

}
catch(Exception ef){
System.out.println();
    ef.printStackTrace();
}

}
}

Stack Traces:

java.io.StreamCorruptedException: invalid type code: 69 at java.base/java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2937) at java.base/java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2971) at java.base/java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:3043) at java.base/java.io.ObjectInputStream.read(ObjectInputStream.java:906) at FileLists.main(createnewfile.java:33)

The problem likely lies here:

while((ch=ois.read())!= -1){
    System.out.println("as");
}

You are writing the 'writeobj' class, followed by the file bytes, and then repeat. So, your saved file will, hopefully, look like:

  • writeobj - written with ObjectOutputStream
  • file bytes - written directly to the FileOutputStream
  • writeobj - written with ObjectOutputStream
  • file bytes - written directly to the FileOutputStream

But, when you are reading, you read one writeobj object, and then attempt to read another object from the ObjectInputStream.

For this to work, you would have to:

  • Read the writeobj from the ObjectInputStream
  • Read the exact number of file bytes directly from the FileInputStream
  • Read the next writeobj from the ObjectInputStream
  • etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM