Java 使用文件读取器读取txt文件中的客户名称
我的问题是,我不知道如何使用文件读取器读取客户名称 我正在预订系统,我需要知道客户已经存在。这就是为什么我必须阅读Customers.txt文件,以便检查是否有人已经是客户。如果他不是,我将用文件编写器制作一个新的。我已经有了代码 这个预约系统的意思是向理发师预约。我必须将预订放在另一个名为reservations.txt的txt文件中,在该文件中,您可以看到每次预订的时间以及谁进行了预订 谢谢你的帮助 这是我已有的代码: 有些评论是荷兰语的,但我会翻译它们Java 使用文件读取器读取txt文件中的客户名称,java,filereader,filewriter,Java,Filereader,Filewriter,我的问题是,我不知道如何使用文件读取器读取客户名称 我正在预订系统,我需要知道客户已经存在。这就是为什么我必须阅读Customers.txt文件,以便检查是否有人已经是客户。如果他不是,我将用文件编写器制作一个新的。我已经有了代码 这个预约系统的意思是向理发师预约。我必须将预订放在另一个名为reservations.txt的txt文件中,在该文件中,您可以看到每次预订的时间以及谁进行了预订 谢谢你的帮助 这是我已有的代码: 有些评论是荷兰语的,但我会翻译它们 package nielbutaye
package nielbutaye;
import java.io.*;
import java.util.UUID;
/**
* @author Niel
*
*/
public class Klant {
//declaration that represents the text
public static String S;
public static String NEWLINE = System.getProperty("line.separator");
/**
* constructor
*/
public Klant(){}
/**
* @return
* By giving the name of the customer you will get all the data from the customer
*/
public double getCustomer() {
return 0 ;
}
/**
* Make a new customer
*/
public void setNew customer(){
// make a newSimpleInOutDialog
SimpleInOutDialog input = new SimpleInOutDialog("A new customer");
//input
S = "Name customer: " + input.readString("Give in your name:");
WriteToFile();
S = "Adress: " + input.readString("Give your adress");
WriteToFile();
S = "Telephonenummber: " + input.readString("Give your telephonenumber");
WriteToFile();
//making a customerID
UUID idCustomer = UUID.randomUUID();
S = "CustomerID: " + customerID.toString();
WriteToFile();
}
public void WriteToFile(){
try{
FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Customer.txt", true);
BufferedWriter out = new BufferedWriter(writer);
//Wrting away your data
out.write(S + NEWLINE);
//Closing the writer
out.close();
}catch (Exception e){//Catch when there are errors
System.err.println("Error: " + e.getMessage());
}
}
}
BufferedReader有一个名为readLine的方法,可用于从文件中读取一行:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
}
br.close();
从WriteToFile方法来看,客户的详细信息似乎占了四行,客户的名字出现在第一行。搜索客户时,将while循环安排为只检查每四行读取的内容
其他要点:
似乎没有理由将S作为成员变量,而不管是静态变量。只需在setNewCustomer中声明一个本地字符串实例,并将其作为参数传递给WriteToFile。
可以使用BufferedWriter的换行方法,而不是定义换行变量。
您能发布到目前为止的代码吗?@hmjd,更新了另一个问题:来自JavaScript标记工具提示:。。。它[JavaScript]与Java不同。谢谢你的回复,你能告诉我在代码中把BufferedWriter的换行方法放在哪里吗,因为我不知道..out.writeS;out.newLine;