Java - 使用Scanner读取大文本文件

发布于 2024-11-19 09:33:38 字数 419 浏览 5 评论 0原文

我有一个非常大的文本文件,其中包含客户信息。我想从文本文件中读取所有客户信息。

这就是我的文本文件的组织方式:

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

Costomer 2:
Name: 
Lars Larsson
Adress:
Street1
Phone number:
085610540

我希望能够读取所有客户信息。有什么好的办法吗?我读过有关扫描仪和图案的内容,想知道在这种情况下使用它们是否是个好主意?我的文本文件非常大,包含数百个客户。

有人知道我如何从文本文件中读取所有信息吗?我创建了一个带有客户变量的类,我只需要帮助读取文本文件。我想以有组织的方式阅读信息。

非常感谢所有帮助。

I have a very big text file with customer information. I would like to read all the customer information from the text file.

This is how my text file is organized:

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

Costomer 2:
Name: 
Lars Larsson
Adress:
Street1
Phone number:
085610540

I would like to be able read all the customer information. Is there any good way to it with? I have read about Scanner and Pattern and was wondering if it is good idea to use them in this case? My text file is very big and contains hundreds of customers.

Dose any one have any idea how I could read all the information from the text file? I have created a class with customer variabled, I only need help with the reading from the text file. I want to read the information in an organized way.

All help is very very appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-11-26 09:33:38

像这样:

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

请给+1并更正你的硬件哈哈

Like so:

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

Please give +1 and correct for ur hw lol

凉城已无爱 2024-11-26 09:33:38

作为stas答案的一个小扩展:

最初发布的代码不起作用,因为继续 跳过当前循环迭代。因此,除非该行以 "" 开头,否则不会执行任何操作。

As a little extension to stas answer:

The originally posted code doesn't work, because a continue skips the current loop-iteration. So unless the line starts with "", nothing is ever done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文