Java 写入包含现有信息的文件,并逐行插入,每个is都有多个项目,用';分隔';
我试图在用户输入所有字段后将信息写入文本文件。 我的文本文件中已有一些信息 安东尼·杜肯;安东尼;a123;彼得街55号;3321444;签证3213504011223 巴里·布莱克;巴里;a999;佐治街456号;;23239876;签证435677779876 克莱尔·雷格;克莱尔;c678;925爱德华 车道;67893344;万事达卡;223344556677 我想把它们作为字符串输入到我的文本文件中,每一行都有多个由Java 写入包含现有信息的文件,并逐行插入,每个is都有多个项目,用';分隔';,java,file,Java,File,我试图在用户输入所有字段后将信息写入文本文件。 我的文本文件中已有一些信息 安东尼·杜肯;安东尼;a123;彼得街55号;3321444;签证3213504011223 巴里·布莱克;巴里;a999;佐治街456号;;23239876;签证435677779876 克莱尔·雷格;克莱尔;c678;925爱德华 车道;67893344;万事达卡;223344556677 我想把它们作为字符串输入到我的文本文件中,每一行都有多个由分隔的项目 在添加新信息之前,我需要打开文件,阅读其中包含的内容,还是
分隔的项目代码>
在添加新信息之前,我需要打开文件,阅读其中包含的内容,还是只需简单地添加到其中
我已经设置了一个用于输入值的构造函数,但是如何使用它呢?我需要为我的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){
}
}
在添加新信息之前,我需要打开文件,阅读其中包含的内容,还是只需简单地添加到其中?
您可以追加到文件中,但不能在其中间插入字符或行。
例如,您可以使用新建FileWriter(file,true)
如果这是生产代码,而不是简单的练习,我建议您仔细研究。要写入文件,请使用FileOutputStream
检查是否有顺便说一下,这种文件称为CSV逗号分隔值
。已经有图书馆来处理这些问题了。