Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文件中写入更多详细信息_Java - Fatal编程技术网

Java 在文件中写入更多详细信息

Java 在文件中写入更多详细信息,java,Java,实际上我正在处理文件,我已经将一个人的详细信息写入一个文件,现在我想将多个人的详细信息写入该文件 文件,我已经尝试使用for循环,但是已经创建了两个文件,我想在同一个文件中写入多个人的详细信息 import java.util.*; import java.io.*; public class Tourism { String name; String contact_number; String address; String enqui

实际上我正在处理文件,我已经将一个人的详细信息写入一个文件,现在我想将多个人的详细信息写入该文件 文件,我已经尝试使用for循环,但是已经创建了两个文件,我想在同一个文件中写入多个人的详细信息

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

}
import java.util.*;
导入java.io.*;
公务舱旅游{
字符串名;
字符串联系人号码;
字符串地址;
字符串查询(u类),;
字符串Des;
串价;
字符串位置;
字符串包;
扫描仪s=新的扫描仪(System.in);
扫描仪s1=新的扫描仪(System.in);
扫描仪s2=新的扫描仪(System.in);
扫描仪s3=新扫描仪(System.in);
扫描仪s4=新扫描仪(系统英寸);
扫描仪扫描=新扫描仪(System.in);
公共选择{
System.out.println(“=============菜单===========”);
System.out.println(“1.Initiate inquiry”);
System.out.println(“2.view查询”);
System.out.println(“3.exit”);
System.out.println(“输入选项”);
int-ch;
ch=scan.nextInt();
开关(ch){
案例1:启动();
打破
案例2:
视图();
打破
案例3:
系统出口(0);
打破
}
}
公共空间启动(){

对于(int i=1;i使用
文件编写器(字符串文件名,布尔追加)

而不是
**新的FileWriter(“Enquiry.txt”)***


修改后的代码告诉您在布尔表达式为true时追加文本。但在您的情况下,它只是在写入而不是追加下一个输入的数据。希望我的代码对您有所帮助。

在您的迭代中,您只是从控制台获取值并存储到变量,而不是写入文件。当您输入第二组值时t覆盖第一个变量。最后,您从循环中出来并将值写入文件。但是变量只保存最后输入的值。 您可以在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);
    }

}
public void initiate(){
试一试{
BufferedWriter br=新的BufferedWriter(新的FileWriter(“Enquiry.txt”);

对于(int i=1;i)创建了哪两个文件?想知道第二个文件名是什么?