2012-03-06 2 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

자바 스크립트 태그 설명에서 "... 자바 스크립트와 다릅니다." –

답변

0

BufferedReader() 당신이에서 라인을 읽는 데 사용할 수 있습니다 readLine()라는 방법을 가지고 :

은 내가 이미 코드 파일 : 그것은 고객의 세부 사항을 보인다 귀하의 WriteToFile() 방법에서

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

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

br.close(); 

첫 번째 줄에 나타나는 고객의 이름으로, 네 줄을 차지합니다. 고객을 검색 할 때 while 루프를 4 번째 라인마다 검사하도록 배열하십시오.

다른 점 :

  • S는 멤버 변수, 신경 끄시 고 static 수에 대한 이유는 없어 보입니다. 로컬 String 인스턴스를 setNewCustomer()에 선언하고 WriteToFile()에 인수로 전달하십시오.
  • NEWLINE 변수를 정의하는 대신 BufferedWriternewLine() 메서드를 사용할 수 있습니다.
+0

응답 해 주셔서 감사합니다. BufferedWriter의 newLine을 어디에 둘 것인지 알려주실 수 있습니까?() 메서드는 코드에서, 내가 단서가 없기 때문에 .. – PauloPawelo

+0

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