简体   繁体   中英

implementing a write method for binary I/O

I'm trying to do java binary I/O where I write a read and a write function with the given headers. I got the read function to work but here is the spec for the write function.

Now, using the DataOutputStream class in the Java Standard Library, complete the write method so that it writes the values in the specified ArrayList<Object> to a file. The list should only contain Integer and Double objects - if you encounter any other type of object you should throw an IllegalArgumentException (a Java Standard Library exception). In addition, there should only be well-formed Integer - Double groups, ie, one Integer object that specifies how many Double objects follow. If you have missing or extra Double values you should also throw an IllegalArgumentException . You can test your write method by using your well tested read method and seeing if you get the expected results. You can test you logic for throwing IllegalArgumentExceptions by constructing ArrayList<Object> lists with unexpected types or ill-formed int-double groups.

here is the code....

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

public class BinaryReader
{
   private String filename;
   private ArrayList<Object> list;

   public static ArrayList<Object> read(String fileName)
   {
      FileInputStream in = null;
      ArrayList<Object> list = new ArrayList<Object>();

      try
      {
         in = new FileInputStream(fileName);
      }
      catch(FileNotFoundException e)
      {
         System.out.println("File not found");
      }

      DataInputStream data = new DataInputStream(in);

      boolean read = true;
      int number = 0;
      double dbl = 0;

      while(read)
      {
         try
         {
            number = data.readInt();
         }
         catch(EOFException e)
         {
            System.out.println("Caught end of file exception");
            break;
         }
         catch(IOException e)
         {
            System.out.println("Caught");
         }

         list.add(number);

         for(int i = 0; i < number; i++)
         {
            try
            {
               dbl = data.readDouble();
            }
            catch(EOFException e)
            {
               System.out.println("Caught");
            }
            catch(IOException e)
            {
               System.out.println("Caught");
            }

            list.add(dbl);
         }
      }
      return list;
   }

   public static void write(String fileName, ArrayList<Object> list)
   {
      FileOutputStream out = null;

      try
      {
         out = new FileOutputStream(fileName);
      }
      catch(FileNotFoundException e)
      {
         System.out.println("file not found");
      }

      DataOutputStream data = new DataOutputStream(out);
      //int count = 0;
      try
      {//PROBLEM AREAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         for(int i = 0; i < list.size(); i++)
         {
            data.writeInt((Integer)list.get(i));


            for(int j = 0; j < list.size(); j++)
            {
               data.writeDouble((Double)list.get(i));
            }
         }
      }
      catch(IOException e)
      {
         System.out.println("Caught");
      }


   }
}

Check for invalid object can be done by using instanceof :

if((!(list.get(i) instanceof Integer) || !(list.get(i) instanceof Double)){
  throw new IllegalArgumentException("Only integer and Double Allowed");
}

For integer-double pair check once you get an Integer treat its value as expected count, then have an actual count, in following iteration before you encounter Integer again keep incrementing your actual count, when you encounter Integer again check if acutal count == expected count if not, throw exception else reinitialize expectedCount with the new Integer value.

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