2015-01-09 108 views
-2
import java.io.*; 

// A binary stream is a series of data type values 
// To read and write to them you use different methods 
// based on the type of data that you are using 

public class lesson33{ 

    public static void main(String[] args){ 

     // Create an array of type Customer 

     Customer[] customers = getCustomers(); 

     // A DataOutputStream allows you to print 
     // primitive data types to a file 

     DataOutputStream custOutput = createFile("/Users/derekbanas/Documents/workspace3/Java Code/src/customers.dat"); 

     // Enhanced for loop for arrays 
     // Cycles through all of the people in the customers array 

     for(Customer person : customers){ 

      createCustomers(person, custOutput); 

     } 

     // Closes the connection to the DataOutputStream 

     try { 
      custOutput.close(); 
     } catch (IOException e) { 

      System.out.println("An I/O Error Occurred"); 

      // Closes the program 

      System.exit(0); 
     } 

     getFileInfo(); 

    } 

    // class that defines all the fields for my customers 

    private static class Customer{ 

     public String custName; 
     public int custAge; 
     public double custDebt; 
     public boolean oweMoney; 
     public char custSex; 

     // constructor that's called when a customer is made 

     public Customer(String custName, int custAge, double custDebt, boolean oweMoney, char custSex){ 

      this.custName = custName; // String 
      this.custAge = custAge; // Integer 
      this.custDebt = custDebt; // Double 
      this.oweMoney = oweMoney; // Boolean 
      this.custSex = custSex; // Character 

     } 

    } 

    // Creates an array of Customer Objects 

    private static Customer[] getCustomers(){ 

     Customer[] customers = new Customer[5]; 

     customers[0] = new Customer("John Smith", 21, 12.25, true, 'M'); 
     customers[1] = new Customer("Sally Smith", 30, 2.25, true, 'F'); 
     customers[2] = new Customer("Paul Ryan", 21, 0, false, 'M'); 
     customers[3] = new Customer("Mark Jacobs", 21, 3.25, true, 'M'); 
     customers[4] = new Customer("Steve Nash", 21, 5.25, true, 'M'); 

     return customers; 

    } 

    // Create the file and the DataOutputStream that will write to the file 

    private static DataOutputStream createFile(String fileName){ 

     try{ 

      // Creates a File object that allows you to work with files 
      // on the hard drive. There is no difference between File 
      // for character or binary stream writing, or reading 

      File listOfNames = new File(fileName); 

      // FileOutputStream is used to write streams of data to a file 
      // You define whether a new file is created versus appended 
      // to based on if you add a boolean to the FileOutputStream 
      // FileOutputStream(file, true) : Appends to the file 
      // FileOutputStream(file, false) : Creates a new file 

      // BufferedOutputStream gathers all the data and then writes 
      // it all at one time (Speeds up the Program) 
      // DataOutputStream is used to write primitive data to the file 

      DataOutputStream infoToWrite = new DataOutputStream(
      new BufferedOutputStream(
        new FileOutputStream(listOfNames))); 
      return infoToWrite; 
     } 

     // You have to catch this when you call FileWriter 

     catch(IOException e){ 
      System.out.println("eager"); 
      System.out.println("An I/O Error Occurred"); 

      // Closes the program 

      System.exit(0); 

     } 
     return null; 

    } 

    // Create a string with the customer info and write it to the file 

    private static void createCustomers(Customer customer, DataOutputStream custOutput){ 

     try{ 
     // Write primitive data to the file 

     // Writes a String in UTF format 
     custOutput.writeUTF(customer.custName); 

     // Writes an Integer 
     custOutput.writeInt(customer.custAge); 

     // Writes a Double 
     custOutput.writeDouble(customer.custDebt); 

     // Writes a Boolean 
     custOutput.writeBoolean(customer.oweMoney); 

     // Writes a Character 
     custOutput.writeChar(customer.custSex); 

     // You also have writeByte, writeFloat, writeLong 
     // and writeShort 
     } 

     catch(IOException e){ 
      System.out.println("eager 1"); 
      System.out.println("An I/O Error Occurred"); 
      System.exit(0); 

     } 

    } 

    // Read info from the file and write it to the screen 

    private static void getFileInfo(){ 

     System.out.println("Info Written to File\n"); 

     // Open a new connection to the file 

     File listOfNames = new File("/Users/derekbanas/Documents/workspace3/Java Code/src/customers.dat"); 

     boolean eof = false; 

     try { 

      // A DataInputStream object has the methods for reading the data 
      // The BufferedInputStream gathers the data in blocks 
      // FileInputStream gets data from the file 

      DataInputStream getInfo = new DataInputStream(
        new BufferedInputStream(
        new FileInputStream(listOfNames))); 

      // Using a while loop that pulls data until EOFException is thrown 

      while (!eof){ 

       // You have to read data in the exact order it was put in the file 

       String custName = getInfo.readUTF(); 
       int custAge = getInfo.readInt(); 
       double custDebt = getInfo.readDouble(); 
       boolean oweMoney = getInfo.readBoolean(); 
       char custSex = getInfo.readChar(); 

       System.out.println(custName); 
       System.out.println(custAge); 
       System.out.println(custDebt); 
       System.out.println(oweMoney); 
       System.out.println(custSex + "\n"); 

      } 
      getInfo.close(); 
     } // END OF TRY 


     catch (EOFException e) { 

      eof = true; 
     } 

     // Can be thrown by FileInputStream 

     catch (FileNotFoundException e) { 

      System.out.println("Couldn't Find the File"); 
      System.exit(0); 
     } 

     catch(IOException e){ 
      System.out.println("eager 2"); 
      System.out.println("An I/O Error Occurred"); 
      System.exit(0); 

     } 

    } 


} 

產生輸出如下: -我不明白這個程序中發生了什麼問題,請幫助我?

eager 
An I/O Error Occurred 

這個程序的目的是要打印的客戶證書表示DataInputStream所

二進制流是一系列數據類型的值 //要讀取和寫給他們你使用不同的方法 //根據你使用的數據類型

+3

那麼你認爲什麼是錯的?你沒有告訴我們。 – 2015-01-09 20:48:03

+2

你抓住了,但你爲什麼不打印堆棧跟蹤?這就是所有信息都隱藏起來的地方! – MightyPork 2015-01-09 20:51:15

+0

我們沒有辦法告訴你什麼時候吞下例外。可能是任何事情。 – 2015-01-09 20:52:10

回答

0

分析:

在IDE(Eclipse,IntelliJ)中以調試模式啓動。我重新創建了錯誤,但這是因爲fileName不存在。

此文件是否存在於您的系統上?這個文件的權限是否正確?這個文件裏有什麼?

  • /用戶/ derekbanas /文檔/ workspace3/Java的代碼/ src目錄/ customers.dat

這是在Unix?你得到像「渴望一個I/O錯誤出現」

+0

感謝您的幫助獲得瞭解決方案。 – eager 2015-01-09 21:18:30

0

原因是由於「java.io.FileNotFoundException:customers.dat(系統找不到指定的路徑)」和CreateFile函數的catch塊顯示該錯誤消息。

問題是文件位置/路徑。

DataOutputStream custOutput = createFile("C:\\tmp\\customers.dat");

請相應地更改文件路徑。