2017-04-14 27 views
-1

我輸入客戶詳細信息並將其保存到文件中。當我打印出來時,只有第一位客戶正在打印。爲什麼會發生這種情況,我該如何解決?JAVA:嘗試輸出一組客戶,但僅打印第一個客戶

import java.util.Scanner; 


public class Main { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 


      Customer e = new Customer(); 
      Scanner scan = new Scanner(System.in); 
      System.out.println(""); 
      e.setCustID(scan.nextLong()); 
      e.setName(scan.next()); 
      e.setSurname(scan.next()); 

      try { 
       FileOutputStream fileOut = new FileOutputStream(new File("customer.txt"),true); 
       ObjectOutputStream out = new ObjectOutputStream(fileOut); 
       out.writeObject(e); 
       out.close(); 
       fileOut.close(); 
       System.out.printf("Serialized data is saved in /tmp/employee.ser\n\n"); 
      }catch(IOException i) { 
      i.printStackTrace(); 
      } 


      try { 
       FileInputStream fileIn = new FileInputStream("customer.txt"); 
       ObjectInputStream in = new ObjectInputStream(fileIn); 
       e = (Customer) in.readObject(); 
       in.close(); 
       fileIn.close(); 
      }catch(IOException i) { 
       i.printStackTrace(); 
       return; 
      }catch(ClassNotFoundException c) { 
       System.out.println("Customer class not found"); 
       c.printStackTrace(); 
       return; 

      } 

      File file = new File("customer.txt"); 
      if(file.exists()) { 


       StringBuilder inputStringBuilder = new StringBuilder(); 
       FileInputStream fileReader = new FileInputStream(file); 
       BufferedReader buffReader = new BufferedReader(new InputStreamReader(fileReader, "UTF-8")); 

       String line = buffReader.readLine(); 

       while(line != null) { 

        System.out.print(e.getCustID()+"\t"); 
        System.out.print(e.getName()+"\t"); 
        System.out.print(e.getSurname()+"\n"); 
        line=buffReader.readLine(); 

       } 




       //if line is null, we have reached EOF (end of file) 
       buffReader.close(); 
      } 
      else { 
       System.out.println("File not found!"); 
      } 


      System.out.println("Deserialized Employee..."); 
      System.out.println("Name: " + e.getCustID()); 
      System.out.println("Address: " + e.getName()); 
      System.out.println("SSN: " + e.getSurname()); 

      // http://www.tutorialspoint.com/java/java_serialization.htm 
      // to try http://www.w3resource.com/java-tutorial/writing-the-file.php 
     } 





} 
+0

用「而((行= buffReader.readLine())!= NULL)」只閱讀文本文件的每一行的第一個條目。但是在每一行中都有多個條目並且輸入是序列化的 –

+0

是正確寫入file.com的數據嗎? –

+0

文件中的數據示例:¬ísr Customer÷w¬; RZ + L Namet Ljava/lang/String; L Surnameq〜 –

回答

0

你的主要問題是,它已被關閉後,你不能追加對象到一個ObjectOutputStream(即一旦運行該程序後)。因此,您需要在初始程序運行期間輸入所有客戶數據。如果您需要多次運行程序,那麼您需要尋找其他解決方案,以便如何將新數據寫入新的ObjectOutputStream等。

如果您很高興在一個程序運行中輸入所有客戶數據,那麼以下簡化代碼示例應該工作:

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.EOFException; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) throws IOException { 

     FileOutputStream fileOut = new FileOutputStream(
       new File("customer.txt")); 
     ObjectOutputStream out = new ObjectOutputStream(
       new BufferedOutputStream(fileOut)); 

     Scanner scan = new Scanner(System.in); 
     try { 

      boolean writing = true; 
      while (writing) { 
       Customer e = new Customer(); 

       System.out.println("Enter Customer Details:"); 
       e.setCustID(scan.nextLong()); 
       e.setName(scan.next()); 
       e.setSurname(scan.next()); 
       out.writeObject(e); 
       System.out.println("Enter More Details ? [Y|N]:"); 
       writing = scan.next().equalsIgnoreCase("y") ? true : false; 
      } 
     } catch (Exception i) { 
      i.printStackTrace(); 
     } finally { 
      out.flush(); 
      out.close(); 
      scan.close(); 
     } 

     File file = new File("customer.txt"); 
     FileInputStream fileIn = new FileInputStream(file); 
     ObjectInputStream in = new ObjectInputStream(
       new BufferedInputStream(fileIn)); 

     while (true) { 

      Customer c; 
      try { 
       c = (Customer) in.readObject(); 
       System.out.print(c.getCustID() + "\t"); 
       System.out.print(c.getName() + "\t"); 
       System.out.print(c.getSurname() + "\n"); 
      } catch (EOFException e1) { 
       break; 
      } catch (ClassNotFoundException e1) { 
       e1.printStackTrace(); 
      } 
     } 

     in.close(); 
    } 

} 
+0

我需要做的是每次運行程序時輸入的數據。之前的inouts必須在文本文件中可用。此外,我需要通過唯一ID來尋找客戶:在這種情況下,這是custID。適用於後一種情況適合什麼? –

+0

有什麼建議嗎? –

+0

@mariusmifsud,每次運行編程時,都需要讀取文件中保存的任何現有數據,並在輸入任何新的「客戶」細節之前將其寫入新的ObjectOutputStream,然後再保存流等等(效率不高做)。對於第二部分來說,一個簡單的方法是循環所有選擇匹配custID的客戶(如果custID應該是唯一的,那麼在找到匹配項後就可以分解)。 –

相關問題