2012-03-06 11 views
0

私の問題はです。ファイルリーダーで顧客名を読み取る方法がわかりません。ファイルリーダーでtxtファイルの顧客名を読む

私は予約システムを作成しており、顧客が既に存在することを知る必要があります。だから私は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()); 
    } 
    } 
} 
+0

これまで持っていたコードを投稿できますか? – hmjd

+0

@hmjd、更新された質問(: – PauloPawelo

+1

JavaScriptタグのツールチップから: "... [JavaScript]はJavaと同じではありません" –

答えて

0

BufferedReader()は、あなたがAから行を読み取るために使用することができますreadLine()という名前のメソッドを持っていますファイル:

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

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

br.close(); 

顧客の詳細は、最初の行に表示される顧客の名前で、4本のラインを占有しているようで、あなたのWriteToFile()方法から。顧客を検索するときは、whileループを4行おきに読み取るようにしてください。

その他の見所には:

  • Sは、メンバ変数、用事staticするために理由はないように見えます。ローカルのStringインスタンスをsetNewCustomer()に宣言し、WriteToFile()の引数として渡してください。
  • NEWLINE変数を定義する代わりに、BufferedWriternewLine()メソッドを使用できます。
+0

お返事ありがとうございます。BufferedWriterのnewLineをどこに置くか教えてください私は手がかりがないので、コード内で()メソッドを使用しています.. – PauloPawelo

+0

'out.write(S); out.newLine();' – hmjd

関連する問題