Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Serializable类仅输出第一条记录_Java_Loops_Serialization - Fatal编程技术网

Java Serializable类仅输出第一条记录

Java Serializable类仅输出第一条记录,java,loops,serialization,Java,Loops,Serialization,我试图创建一个循环来显示二进制文件中的所有内容,但它只显示第一件事: public class ItemRecordReport { static Scanner input; //allows for the scanner to be availble to other methods in the class static ObjectOutputStream binOutFile; static ObjectInputStream binInFile; pub

我试图创建一个循环来显示二进制文件中的所有内容,但它只显示第一件事:

public class ItemRecordReport
{
   static Scanner input; //allows for the scanner to be availble to other methods in the class
   static ObjectOutputStream binOutFile; 
   static ObjectInputStream binInFile;

   public static void main(String args[]) throws IOException, ClassNotFoundException
   {
      // Create a scanner
      try
      {
         input = new Scanner(new File ("ItemRecord_text.txt")); //use scanner input
      } 
      catch (FileNotFoundException fileNotFoundException)
      {
         System.err.println("Error opening file.");
         System.exit(1);
      }
      //create an object output stream
      try
      {
         binOutFile = new ObjectOutputStream (new FileOutputStream("ItemRecord_binary.txt"));
      }
      catch(FileNotFoundException fileNotFoundException)
      {
         System.err.println("Error opening file.");
         System.exit(1);
      }
       //Create an Item Record Object
      ItemRecord record = new ItemRecord("null", 0.0, 0, "null");


      //Create a loop that will read from the file, and store it in an ItemRecord object

      while (input.hasNext() )
      {
         record.setItemNumber(input.next() );
         record.setCost(input.nextDouble() );
         record.setQuantity(input.nextInt() );
         record.setDescription(input.nextLine());


         binOutFile.writeObject(record); //stores the file in binary

      }
      binOutFile.close(); //close the file

      //Create an objectinputstrea
      binInFile = new ObjectInputStream ( new FileInputStream("ItemRecord_binary.txt") );

      //create a loop that stores the binary file in an item record object
      try
      {
         while (true)
         {
         //Reads an obejct from the binary file
            record = (ItemRecord) binInFile.readObject(); //read the object into record

            System.out.println(record.toString() );

         }
       }
      catch (EOFException endOfFileException)
      {
         return;
      }
每次迭代都需要创建一个新的
ItemRecord
对象,因为序列化不会重新序列化已序列化对象的内容。或:

  • 使用
    writeUnshared()
    而不是
    writeObject()
  • 在每次
    writeObject()之后调用
    ObjectOutputStream.reset()

请将这张难以辨认的表格格式化。