2014-04-27 25 views
0

寫入文件我有我的主要變量,並且我想使用一個私有(或公共並不重要,我保持它們在同一個類中)方法來編寫它們到一個文本文件。我已經完成了將它們寫入主文件...我只是無法弄清楚如何從main調用writeToFile()方法中的變量。以下是我所嘗試的,但我不知道如何將兩者結合起來。使用變量從主要的

//This portion is what I had in my main method that wrote the info to a file successfully 

    //Write to File 
      String fileName = "order.txt"; 
      try{ 
       PrintWriter writer = new PrintWriter(fileName); 
       writer.println("Thank you for ordering from Diamond Cards"); 
       writer.println("Name: " + customerName); 
       writer.println("Returning Customer: " + customerReturn); 
       writer.println("Phone: " + custNumber); 
       writer.println("Card Type: " + customerType); 
       writer.println("Card Color: " + customerColor); 
       writer.println("Card Coating: " + customerCoat); 
       writer.println("Item Amount: " + numItems); 
       writer.println("Total Cost: " + fmt1.format(totalCostMsg)); 
       writer.flush(); 
       writer.close(); 
       JOptionPane.showMessageDialog(null, "Receipt has been printed"); 
      } catch (FileNotFoundException e) 
         {e.printStackTrace(); 
         System.exit(0) ;   
         }  
      } 


// This is where I try to create a method to do the file writing.... not sure how to proceed.. 


    public static void writeToFile() { 
      try{ 
       FileWriter fw = new FileWriter("order.text"); //File name to be created 
       PrintWriter pw = new PrintWriter (fw);   // Prints to the file that was created 
     //text to be printed to file 

     // close the writer  
       pw.close(); 
     // catch errors  
       } catch (IOException e) { 
       out.println("Error!"); 
       } 
      } 

我還需要弄清楚如何使一個單獨的方法來讀取文件回,但我想我可以工程師,如果我可以算出這個部分了。

+0

只需在'writeToFile()'中添加所需的參數,以便您可以將它們傳入... –

+0

這樣做? writeToFile(customerName,numItems,..... blah)? – user3537993

回答

1

你要定義writeToFile帶參數,並從main在通過他們:

// Add additional arguments. 
    public static void writeToFile(String fileName, String customerName, ...){ 
     FileWriter fw = new FileWriter(fileName); 
     writer.println("Thank you for ordering from Diamond Cards"); 
      writer.println("Name: " + customerName); 
     // Use additional arguments. 
    } 

從主:

writeToFile(fileName, customerName, ...); 

我Polywhirl先生雖然同意。如果你創建一個包裝對象,它會更乾淨,但我不確定你爲此需要getterssetters

// The types are all String because you did not mention the types in your 
// question. 
class Customer { 
    public String Name; 
    public String Return; 
    public String Number; 
    public String Type; 
    public String Color; 
    public String Coat; 
    public Customer String(String Name, String Return, String Number, String Type, String Color, String Coat) { 
     this.Name = Name; 
     this.Return = Return; 
     this.Number = Number; 
     this.Type = Type; 
     this.Color = Color; 
     this.Coat = Coat; 

    } 
} 

然後,您可以做main如下:

Customer c = new Customer(customerName, customerReturn, customerNumber, customerType, customerColor, customerCoat); 

裏面的writeToFile方法具有相同簽名的Polywhirl先生的回答,您可以直接做customer.Name

1

你可以通過向您的方法添加參數來傳遞對象。如果您需要引用其他類或方法中的某些內容,只需添加更多參數即可。

我建議您創建一個Customer對象,以便您可以將它作爲單個實體傳遞,而不是幾十個參數。

你可以嘗試這樣的事情:

public class FileWriteExample { 
    public static void main(String[] args) { 
     String fileName = "order.txt"; 
     Customer customer; // Customer object... 
     int itemCount; 
     float totalCost; 
     try { 
      PrintWriter writer = new PrintWriter(fileName); 
      writeToFile(writer, customer, itemCount, totalCost); 
      writer.flush(); 
      writer.close(); 
      JOptionPane.showMessageDialog(null, "Receipt has been printed"); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 
    public static void writeToFile(PrintWriter writer, Customer customer, 
      int itemCount, float totalCost) { 
     Card card = customer.getCard(); 
     try { 
      writer.println("Thank you for ordering from Diamond Cards"); 
      writer.println("Name: " + customer.getName()); 
      writer.println("Returning Customer: " + customer.getReturn()); 
      writer.println("Phone: " + customer.getPhone()); 
      writer.println("Card Type: " + card.getType()); 
      writer.println("Card Color: " + card.getColor()); 
      writer.println("Card Coating: " + card.getCoating()); 
      writer.println("Item Amount: " + itemCount); 
      writer.println("Total Cost: " + fmt1.format(totalCost)); 
     } catch (IOException e) { 
      System.out.println("Error!"); 
     } 
    } 
} 
0

可能有一些領域不需要的或不可用,如聯絡電話號碼等。而不是發送一長串寫入文件,可以考慮使用生成器模式,如Joshua Bloch在Effective Java中所建議的。