2016-03-07 88 views
-1

實際上我工作的文件,我已經寫了正好一個人的細節到一個文件中,現在我想寫多的人的詳細信息到 文件,我已經使用循環嘗試,但兩個文件已創造,我想在同一文件中使用,而不是在文件中寫入詳細信息

import java.util.*; 
    import java.io.*; 
    public class Tourism { 
    String name; 
    String contact_number; 
    String address; 
    String enquiry_category; 
    String Des; 
    String price; 
    String location; 
    String packages; 
    Scanner s=new Scanner(System.in); 
    Scanner s1=new Scanner (System.in); 
    Scanner s2=new Scanner(System.in); 
    Scanner s3=new Scanner(System.in); 
    Scanner s4=new Scanner(System.in); 
    Scanner scan=new Scanner(System.in); 
    public void Choice(){ 
    System.out.println("========menu========"); 
     System.out.println("1.Initiate enquiry"); 
     System.out.println("2.view enquiry"); 
     System.out.println("3.exit"); 
     System.out.println("enter the choice"); 
     int ch; 
     ch=scan.nextInt(); 
     switch(ch){ 
      case 1:initiate(); 
       break; 
      case 2: 

      View(); 
       break; 

      case 3: 
       System.exit(0); 
       break; 
     } 
    } 
    public void initiate(){ 
     for(int i=1;i<=2;i++){ 
      System.out.println("=========="+i+"========="); 
     System.out.println("enter the name"); 
     name=s.next(); 
     System.out.println("enter the contact number"); 
     contact_number=s1.nextLine()+""; 
     System.out.println("enter the address"); 
     address=s2.nextLine()+""; 
     System.out.println(" enquiry categories:-"); 
     System.out.println("enter the price range"); 
     price=s1.nextLine()+""; 
     System.out.println("enter the location"); 
     location=s2.nextLine()+""; 
     System.out.println("select/enter the package u want to have"); 
     packages=s3.nextLine()+""; 
     System.out.println("enter the description of enquiry"); 
     Des=s4.nextLine()+""; 
     } 
     try{ 
      BufferedWriter br=new BufferedWriter(new FileWriter("Enquiry.txt")); 
      br.write(name); 
      br.newLine(); 
      br.write("mobile number:"+contact_number); 
      br.newLine(); 
      br.write("address:"+address); 
      br.newLine(); 
      br.write("price:"+price); 
      br.newLine(); 
      br.write("location:"+location); 
      br.newLine(); 
      br.write("packages:"+packages); 
      br.newLine(); 
      br.write("enquiry description:"+Des); 
      br.close(); 
     }catch(IOException e){ 
      System.out.println(e); 
     } 
    } 

    public void View(){ 
     Scanner scanner=new Scanner(System.in); 
     System.out.println("enter the name to view the details"); 
     String name1; 
     name1=scanner.nextLine(); 
     try{ 
     BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\shashi.s\\Documents\\NetBeansProjects\\JavaApplication128\\Enquiry.txt")); 
     String line; 
     while((line=br.readLine())!=null){ 
     if(line.equals(name1)){ 
      System.out.println(line); 
      String line1; 
      while((line1=br.readLine())!=null){ 
      System.out.println(line1); 
     } 
     }else{ 
      System.out.println("oops "+name1+" .....does not exist"); 
      break; 
     } 
     } 
     }catch(IOException e){ 
      System.out.println(e); 
     } 
    } 
    public static void main(String[] args) { 
     Tourism t=new Tourism(); 
     t.Choice(); 
    } 

} 
+2

哪兩個文件被創建? – shmosel

+0

https://www.coursera.org/specializations/java-programming – wildloop

+1

疑惑是什麼,第二個文件的名字嗎? – Yazan

回答

0

FileWriter(String fileName, boolean append)

寫多個人的詳細信息**new FileWriter("Enquiry.txt")**

當布爾表達式爲真時,修改的代碼告訴附加文本。但在你的情況下,它只是寫的,而不是下一個追加進入data.Hope你找到我的代碼有幫助。

0

在迭代中,您只是從控制檯獲取值並將其存儲到變量中,但不寫入文件。當輸入第二組值時,它會覆蓋第一個值。最後,您將從循環中跳出並將這些值寫入file.But變量只保存最後輸入的值。 您可以修改您的代碼以寫入文件,在for循環本身內,修改如下:

public void initiate() { 
    try { 
     BufferedWriter br = new BufferedWriter(new FileWriter("Enquiry.txt")); 
     for (int i = 1; i <= 2; i++) { 
      System.out.println("==========" + i + "========="); 
      System.out.println("enter the name"); 
      name = s.next(); 
      System.out.println("enter the contact number"); 
      contact_number = s1.nextLine() + ""; 
      System.out.println("enter the address"); 
      address = s2.nextLine() + ""; 
      System.out.println(" enquiry categories:-"); 
      System.out.println("enter the price range"); 
      price = s1.nextLine() + ""; 
      System.out.println("enter the location"); 
      location = s2.nextLine() + ""; 
      System.out.println("select/enter the package u want to have"); 
      packages = s3.nextLine() + ""; 
      System.out.println("enter the description of enquiry"); 
      Des = s4.nextLine() + ""; 
      br.newLine(); 
      br.write(name); 
      br.newLine(); 
      br.write("mobile number:" + contact_number); 
      br.newLine(); 
      br.write("address:" + address); 
      br.newLine(); 
      br.write("price:" + price); 
      br.newLine(); 
      br.write("location:" + location); 
      br.newLine(); 
      br.write("packages:" + packages); 
      br.newLine(); 
      br.write("enquiry description:" + Des); 
     } 
     br.close(); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 

}