Java-使用扫描仪读取大文本文件

Java-使用扫描仪读取大文本文件,java,java.util.scanner,Java,Java.util.scanner,我有一个很大的客户信息文本文件。我想从文本文件中读取所有客户信息 以下是我的文本文件的组织方式: Costomer 1: Name: Erik Andersson Adress: Street1 Phone number: 085610540 Costomer 2: Name: Lars Larsson Adress: Street1 Phone number: 085610540 我希望能够阅读所有的客户信息。有没有什么好办法?我读过关于扫描仪和模式的书,想知道在这种情况下使用它们是否

我有一个很大的客户信息文本文件。我想从文本文件中读取所有客户信息

以下是我的文本文件的组织方式:

Costomer 1:
Name: 
Erik Andersson
Adress:
Street1
Phone number:
085610540

Costomer 2:
Name: 
Lars Larsson
Adress:
Street1
Phone number:
085610540
我希望能够阅读所有的客户信息。有没有什么好办法?我读过关于扫描仪和模式的书,想知道在这种情况下使用它们是否是个好主意?我的文本文件非常大,包含数百名客户

有人知道我怎样才能从文本文件中读取所有信息吗?我已经创建了一个带有customer变量的类,我只需要从文本文件读取的帮助。我想有条理地阅读这些信息

非常感谢您的帮助。

如是:

public void getEmployees(File f) throws Exception {
    // An ArrayList of your Employee-Object to hold multiple Employees
    ArrayList<Employee> employees = new ArrayList<Employee>();
    // The reader to read from your File
    BufferedReader in = new BufferedReader(new FileReader(f.getAbsolutePath()));
    // This will later contain one single line from your file
    String line = "";

    // Temporary fields for the constructor of your Employee-class
    int number;
    String name;
    String adress;
    String phone;

    // Read the File untill the end is reached (when "readLine()" returns "null")
    // the "line"-String contains one single line from your file.
    while ( (line = in.readLine()) != null ) {
        // See if your Line contains the Customers ID:
        if (line.startsWith("Customer")) {
            // Parse the number to an "int" because the read value
            // is a String.
            number = Integer.parseInt(s.substring("Customer ".length()).substring(0,s.indexOf(':')));
        } else if (line.startsWith("Adress:")) {
            // The Adress is noted in the next line, so we
            // read the next line:
            adress = in.readLine();
        } else if (line.startsWith("Phone number:")) {
            // Same as the Adress:
            phone = in.readLine();
        } else if (line.startsWith("Name:")){
            // Same as the Adress:
            name = in.readLine();
        } else if ( line.equals("") ){
            // The empty line marks the end of one set of Data
            // Now we can create your Employee-Object with the
            // read values:
            employees.add(new Employee(number,name,adress,phone));      
        }
    }
    // After we processed the whole file, we return the Employee-Array
    Employee[] emplyeeArray = (Employee[])employees.toArray();
}
public void getEmployees(文件f)引发异常{
//用于容纳多个员工的员工对象的ArrayList
ArrayList employees=新的ArrayList();
//要从文件中读取的读取器
BufferedReader in=新的BufferedReader(新文件读取器(f.getAbsolutePath());
//稍后将包含文件中的一行
字符串行=”;
//Employee类的构造函数的临时字段
整数;
字符串名;
字符串地址;
字符串电话;
//读取文件直到到达结尾(当“readLine()”返回“null”时)
//“line”-字符串包含文件中的一行。
而((line=in.readLine())!=null){
//查看您的行是否包含客户ID:
if(第行开始与(“客户”)){
//将数字解析为“int”,因为读取值
//这是一根绳子。
number=Integer.parseInt(s.substring(“Customer.length()).substring(0,s.indexOf(“:”)));
}else if(第行startsWith(“地址:)){
//地址记在下一行,所以我们
//阅读下一行:
地址=in.readLine();
}else if(line.startsWith(“电话号码:”)){
//与地址相同:
phone=in.readLine();
}else if(line.startsWith(“Name:”)){
//与地址相同:
name=in.readLine();
}else if(行等于(“”){
//空行表示一组数据的结束
//现在,我们可以使用
//读取值:
添加(新员工(号码、姓名、地址、电话));
}
}
//处理完整个文件后,返回Employee数组
Employee[]employeearray=(Employee[])employees.toArray();
}
请给出+1并更正您的hw lol

作为回答的一个小扩展:

最初发布的代码不起作用,因为
continue
跳过当前循环迭代。因此,除非该行以“
”开头,否则将不会执行任何操作。但是不要投我反对票,因为你的想法是对的

我更新了代码(没有测试),现在应该可以了

如果你发布代码来回答一个问题,你应该把它注释掉,这样读者就能理解它的作用


另外,不要乞求名誉,这是不礼貌的。

你对Java很陌生吗?这是作业吗?“成百上千的顾客”一点也不重要。@GregS-是的,这是作业。@Lars012:没问题,但在问题中加上作业标签被认为是礼貌的。我已经这样做了。cmon我为你做了所有的工作,至少给我信用,因为它没有工作,我更新了你的代码。也请看我的帖子以获得一些建议。答案是Thnks。我想知道我怎么知道我的客户什么时候被阅读,下一个应该什么时候开始?因为有时客户信息之间会有一些空行。如果一个地址分布在多行上会怎么样?我该怎么处理呢?如果有空行,它已经处理了,只保留一行地址。谢谢你的回答。我在想,如果客户信息是通过多行传输的,我该如何处理?例如,如果地址分布在3行上,然后电话号码行出现。如果您对文件格式有影响,请使用或。它们都受到Java的大力支持。