0

我的问题是我无法弄清楚如何使用文件阅读器读取客户姓名。

我正在制作预订系统,我需要知道客户已经存在。这就是为什么我必须阅读我的 Customers.txt 文件,以便检查某人是否已经是客户。如果他不是,我将使用 File writer 制作一个新的(我已经有了代码)。

这个预约系统的意思是和理发师预约。我必须将预订放在另一个名为 Reservations.txt 的 txt 文件中,在该文件中,您可以看到每个预订时间以及谁进行了预订。

谢谢您的帮助!

这是我已经拥有的代码:(一些评论是荷兰语,但我会翻译它们)

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());
    }
    }
}
4

1 回答 1

0

ABufferedReader()有一个名为 的方法readLine(),您可以使用该方法从文件中读取一行:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
}

br.close();

从您的WriteToFile()方法看来,客户的详细信息占据了四行,客户的姓名出现在第一行。搜索客户时,将while循环安排为仅检查每四行读取一次。

其他要点:

  • 似乎没有理由S成为成员变量,更不用说static. 只需在其中声明一个本地String实例setNewCustomer()并将其作为参数传递给WriteToFile().
  • NEWLINE您可以使用BufferedWriter'newLine()方法来代替定义变量。
于 2012-03-06T17:52:06.797 回答