在用户输入所有字段后,我试图将信息写入文本文件。我的文本文件中已经有一些信息。
安东尼·杜肯;安东尼;a123;55 彼得街;3321444;VISA;3213504011223 巴里·布莱克;巴里;a999;456 乔治街;23239876;VISA;435677779876 克莱尔·雷格;克莱尔;c678;925 爱德华·莱恩;67893344;MASTERCARD;2233445
我想将它们作为字符串输入到我的文本文件中,并且每行都有多个项目,由;
.
在添加新信息之前我是否需要打开我的文件,阅读其中包含的内容,或者我可以简单地添加到其中?
我已经设置了一个用于输入值的构造函数,但是如何使用它呢?我需要为我的 main 制定一个新方法来调用它吗?
import java.io.*;
import java.util.*;
public class newCust{
String name,username,password,address,contact,creditType,creditNum;
Scanner input = new Scanner(System.in);
public newCust(String name , String username, String password, String address, String contact, String creditType, String creditNum){
this.name= name;
this.username= username;
this.password = password;
this.address = address;
this.contact = contact;
this.creditType = creditType;
this.creditNum = creditNum;
System.out.println("Welcome to Kreg Hotel Booking System");
System.out.println("==============================================");
System.out.println("Please enter your name: ");
name = input.nextLine();
System.out.println("Please enter your login username: ");
username = input.nextLine();
System.out.println("Please enter your login password: ");
password = input.nextLine();
System.out.println("Please enter your address: ");
address = input.nextLine();
System.out.println("Please enter your contact: ");
contact = input.nextLine();
System.out.println("Please enter your credit card type: ");
creditType = input.nextLine();
System.out.println("Please enter your credit card number: ");
creditNum = input.nextLine();
try{
PrintWriter fileout = new PrintWriter("customerinfo");
newCust info = new newCust(name,username,password,address,contact,creditType,creditNum);
fileout.print(info);
}
catch(IOException ex){
}
}
public static void main(String[] args){
}
}