我有来自我的main的变量,我想使用私有(或公共并不重要,我保持它们在同一个类中)方法将它们写入文本文件。我已经完成了将它们写入主文件中的文件...我只是想弄清楚如何从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 :(得分: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.
}
来自main:
writeToFile(fileName, customerName, ...);
我同意Polywhirl先生的意见。如果你创建一个包装对象会更干净,虽然我不确定你为此需要getters
和setters
。
// 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 :(得分: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!");
}
}
}
答案 2 :(得分:0)
可能有一些不需要或不可用的字段,如联系号码等。不要发送长列表写入文件,而应考虑使用Joshua Bloch在Effective Java中建议的Builder模式。