-4

How to read the multiple objects in a text file. I am trying to read the text file. I am always get only the first object of the file. How to get the all objects from text file...

List<Processedfile> processfiles = new ArrayList<Processedfile>();

        Processedfile processfile = new Processedfile(); 
        processfile.setFilename(filename);
        processfile.setCountrow(uploadedFileCount);
        processfile.setDate(dateformat);

        processfiles.add(processfile);

        writeReportTextFile(processfiles);

Write the processedfile object in the text file...

Write the file

public void writeReportTextFile(List<Processedfile> processfiles) {

        String processedfilereport = "D:\\PaymentGatewayFiles\\MSSConsolidate\\processedfilereport.txt";

        try {
            File file = new File(processedfilereport);
            FileOutputStream f = new FileOutputStream(file.getAbsoluteFile(), true);
            // System.out.println(file);
            ObjectOutputStream s = new ObjectOutputStream(f);
            // System.out.println("the write"+reportfile);
            s.writeObject(processfiles);

            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Read the file..

public List<Processedfile> processreportfileread() {

        List<Processedfile> a1 = new ArrayList();
        String processedfilereport = "D:\\PaymentGatewayFiles\\MSSConsolidate\\processedfilereport.txt";
    try {
        File file = new File(processedfilereport);

         FileInputStream r = new FileInputStream(file);
     ObjectInputStream sp = new ObjectInputStream(r);

         a1 = (List) sp.readObject();

         System.out.println("the list is" +a1);

         Iterator i = a1.iterator();
         while(i.hasNext()) {

             System.out.println("the iterator report is ===="+i.next());
         }

         }
        catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return a1;
    }
Madhavaraman
  • 51
  • 1
  • 7
  • 1
    Please, add some code. Show us the code you're trying to achieve with what you'e subscribed above and mark the line where you got stuck. –  Apr 14 '18 at 06:24
  • Good question and we'll most likely never be able to tell you why you're having this problem unless you post the code you are using and provide an example of what the text file contains. – DevilsHnd - 退職した Apr 14 '18 at 06:26
  • 1
    1. What you're writing/reading is not a text file. It contains a binary, serialized representation of a list of objects. 2. Yo're only adding a single Processedfile to the list you're writing to the file. So why do you expect multiple objects when reading it back? 3. Dont use raw types. Use List, and Iterator instead of List and Iterator. – JB Nizet Apr 14 '18 at 06:35
  • every time i write the file name and countrow..another file comes then add the file name and countrow at same text file.. so i want to display the text file information to frontend. – Madhavaraman Apr 14 '18 at 06:43
  • Answer that question: *You're only adding a single Processedfile to the list you're writing to the file. So why do you expect multiple objects when reading it back?* – JB Nizet Apr 14 '18 at 06:52
  • ok.. first time is single processed file.. every time file comes its append to the text file.. ..if suppose i add the multiple processedfile to the list. but i read the file only return the first object only. But the text file contains multiple objects.. – Madhavaraman Apr 14 '18 at 06:59
  • You don't want to append a new list to the file. You want to add an element to the list, and then overwrite the file with the content of the list. – JB Nizet Apr 14 '18 at 07:06
  • ok.. i have multiple objects in the text file.. how to send frondend.. – Madhavaraman Apr 14 '18 at 07:13
  • how to convert the excel sheet to json format in java?? – Madhavaraman Apr 18 '18 at 10:24

1 Answers1

0

The object serialization protocol / APIs can cope with a stream containing single object or a sequence of objects. But it cannot cope with a concatenation of streams1 ... which is what your application appears to be creating by opening the output file in "append" mode)

The solution is to NOT write them like that. This:

  FileOutputStream f = new FileOutputStream(file.getAbsoluteFile(), true);

is fundamentally wrong. It results in multiple object streams being concatenated. That can't be read.

The correct approach is to either:

  1. assemble all of the Processedfile objects into a single list, and write the list ... with one writeObject call, or
  2. use a sequence of writeObject calls to write the Processedfile objects to a single ObjectOutputStream, or
  3. write the objects to multiple output files, and send the files as a ZIP archive or similar.

1 - To understand why, you need to read the Object Serialization specification, and in particular you need to understand the serialization protocol / format.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for the reply.... once i write the text the previous record was not there.. i want to write without delete the previous record... – Madhavaraman Apr 14 '18 at 09:42
  • So, I take it that you are running the application multiple times, and each time you append a record. In that case you MUST use option 3 ... or something similar. – Stephen C Apr 14 '18 at 11:45
  • how to convert the excel sheet to json format in java?? – Madhavaraman Apr 18 '18 at 10:24