简体   繁体   English

实现二进制I / O的写方法

[英]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. 我正在尝试做Java二进制I / O,在其中我编写了具有给定标头的读取和写入功能。 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. 现在,使用Java标准库中的DataOutputStream类,完成write方法,以便它将指定ArrayList<Object>的值写入文件。 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). 该列表应仅包含IntegerDouble对象-如果遇到任何其他类型的对象,则应引发IllegalArgumentException (Java标准库异常)。 In addition, there should only be well-formed Integer - Double groups, ie, one Integer object that specifies how many Double objects follow. 另外,应该只存在格式良好的Integer - Double组,即,一个Integer对象,指定跟随多少个Double对象。 If you have missing or extra Double values you should also throw an IllegalArgumentException . 如果缺少或有多余的Double值,则还应该抛出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. 您可以通过构造具有意外类型或格式不正确的int-double组的ArrayList<Object>列表来测试引发IllegalArgumentExceptions的逻辑。

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 : 可以通过使用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. 对于整数双对检查,一旦获得整数,就将其值视为预期计数,然后获得实际计数,在随后的迭代中再次遇到整数之前,请继续增加实际计数,当再次遇到整数时,请检查实际计数是否==预期如果不计数,则抛出异常,否则使用新的Integer值重新初始化ExpectedCount。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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