java - 文件编写器无法在 do while 循环中正常工作
问题描述
如果我在它编译的循环内设置构造函数,但它只写入最后一个输入,我无法将数据写入文件。如果将构造函数放在循环之外,则会出现问题,我无法弄清楚如何解决它。我一直在尝试,但没有任何效果
public static void main(String[] args) throws IOException {
boolean isRegularClient;
Scanner myScan = new Scanner (System.in);
int userChoice;
File myFile = new File ("invoices.txt");
try {
FileWriter myWriter = new FileWriter (myFile);}
catch (IOException ex) {
System.out.println("An error occurred.");}
do{
System.out.println ("Welcome to Marco's Candy Shop");
System.out.println ("Our speciality and our unique product is the famous Mysterious Beans.");
System.out.println ("How many lbs do you want? (only exact amounts)");
while (!myScan.hasNextInt()){
myScan.next();
}
int quantityCandy = myScan.nextInt();
System.out.println ("We are giving 20% off to our regular customers. Are you a regular client?");
String string1 = myScan.next();
if ("Yes".equals(string1) || "yes".equals(string1)){
isRegularClient = true;
}
else {
isRegularClient = false;
}
System.out.println ("Please Enter your billing adress");
String adress = myScan.next();
System.out.println ("How much is your local tax rate? (enter as percent)");
while (!myScan.hasNextDouble()){
myScan.next();
}
double taxRate = myScan.nextDouble();
Client newClient = new Client(adress,isRegularClient,quantityCandy,taxRate);
final double pricexLB = 0.80;
final double regularClientDiscount = 0.20;
double orderPrice = (pricexLB * quantityCandy);
double orderTax = (orderPrice*taxRate)/100;
double finalCharge;
if (isRegularClient){
finalCharge = (orderPrice-(orderPrice*regularClientDiscount))+orderTax;
}
else {
finalCharge = orderPrice+orderTax;
}
System.out.println ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
try{
myWriter.write ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
myWriter.close();}
catch (IOException e) {
System.out.println("An error occurred.");}
System.out.println ("If you want to make another order please enter 1, otherwise enter any number to exit");
while (!myScan.hasNextInt()){
myScan.next();}
userChoice = myScan.nextInt();}
while (1 == userChoice);}
}
解决方案
您以写入模式打开 fileWriter,它通常会清空文件并添加新内容。为此,您必须以附加模式打开文件。
FileWriter 将采用两个参数,第二个参数作为附加模式的布尔值,如果您在 try 块内创建文件写入器,它只能在该范围内访问,您无法在外部访问。所以你也必须在 try/catch 中移动 do/while 块。
try {
FileWriter myWriter = new FileWriter (myFile, true);
do{
System.out.println ("Welcome to Marco's Candy Shop");
System.out.println ("Our speciality and our unique product is the famous Mysterious Beans.");
System.out.println ("How many lbs do you want? (only exact amounts)");
while (!myScan.hasNextInt()){
myScan.next();
}
int quantityCandy = myScan.nextInt();
System.out.println ("We are giving 20% off to our regular customers. Are you a regular client?");
String string1 = myScan.next();
if ("Yes".equals(string1) || "yes".equals(string1)){
isRegularClient = true;
}
else {
isRegularClient = false;
}
System.out.println ("Please Enter your billing adress");
String adress = myScan.next();
System.out.println ("How much is your local tax rate? (enter as percent)");
while (!myScan.hasNextDouble()){
myScan.next();
}
double taxRate = myScan.nextDouble();
Client newClient = new Client(adress,isRegularClient,quantityCandy,taxRate);
final double pricexLB = 0.80;
final double regularClientDiscount = 0.20;
double orderPrice = (pricexLB * quantityCandy);
double orderTax = (orderPrice*taxRate)/100;
double finalCharge;
if (isRegularClient){
finalCharge = (orderPrice-(orderPrice*regularClientDiscount))+orderTax;
}
else {
finalCharge = orderPrice+orderTax;
}
System.out.println ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
try{
myWriter.write ("You ordered " + quantityCandy + " lbs of the Mysterious Beans. You have to pay " + finalCharge + "$. This invoice will be sent to " + adress);
myWriter.close();}
catch (IOException e) {
System.out.println("An error occurred.");}
System.out.println ("If you want to make another order please enter 1, otherwise enter any number to exit");
while (!myScan.hasNextInt()){
myScan.next();}
userChoice = myScan.nextInt();}
while (1 == userChoice);}
} catch (IOException ex) {
System.out.println("An error occurred.");
}
请参阅此文档以获取更多信息。