如何在Java中一次读取每一行,保存该行中的信息,然后移动到下一行

问题描述 投票:0回答:1

我遇到了这个问题,我认为这基本上只是算法,但尽管我尝试了多种方法,但我还是无法弄清楚。我有一个文件名“employees.txt”。该文件中的每一行都是一条记录,我想要做的是从此文件中读取一行,提取员工的信息,将该记录保存到列表中,然后移至下一行,重复直到文件末尾.

这是我迄今为止尝试过的。还有一件事是我必须使用 Scanner,而不是其他流阅读器

 public void inputData(Scanner scanner) {
        if (employeeFile.exists()){
            try {
                scanner = new Scanner(employeeFile);
                String newLine = System.getProperty("line.operator");
                StringBuilder str = new StringBuilder();
//                ArrayList<String> data = new ArrayList<>();
                int count = 0;
                while (scanner.hasNextLine()){
                    str.append(scanner.nextLine());
                    System.out.printf("%s + %d\n", str, count);
                    count++;

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else

            System.out.println("File not exists!!!");
    }

这是“employees.txt”中的记录,每条记录以换行符分隔


D, 1, John Smith, 02-12-1971, 42 Victoria St. Hurstville NSW 2456, 10001, AVAILABLE
D, 2, Peter Taylor, 01-12-1970, 42 Victoria St. Hurstville NSW 2456, 10008, ON LEAVE
D, 3, John Doe, 03-23-1966, 12 Station St. Dapto NSW 2530, 10002, AVAILABLE
D, 4, John Gray, 05-05-1988, 16 Station St. Dapto NSW 2530, 10004, AVAILABLE
D, 5, Adam Taylor, 01-01-1980, 42 Church St. City NSW 2300, 10003, ON LEAVE
D, 6, Michael Jones, 03-05-1975, 23 Waterloo Ave. Surry Hills NSW 2502, 10012, AVAILABLE
D, 7, Frederic Jones, 02-10-1978, 3 Victoria St. Redfern NSW 2420, 20002, BUSY
D, 8, Peter O'Brien, 02-28-1983, 19 Lucas Dr. Horsley NSW 2530, 20003, 'BUSY'
A, 9, John Lucas, 12-16-1966, 20 Huxley St. Horsley NSW 2530, SUPPORT
A, 10, John Fox, 10-15-1975, 18 Victoria St. Hurstville NSW 2456, DIRECTOR
D, 11, Adam Fox, 08-10-1974, 45 Victoria St. Hurstville NSW 2456, 30005, BUSY
D, 12, Phillip Cox, 12-12-1985, 5 The Avenue Rockdale NSW 2300, 40002, BUSY
D, 13, Andrew K Smith, 04-04-1969, 42 Bambaramba Ave. Pennant Hills NSW 2556, 20045, AVAILABLE
A, 14, Andrew R Smith, 04-01-1992, 67 King Cr. Hurstville NSW 2456, CEO
A, 15, Michael Potter, 04-01-1995, 568 Bong Bong St. Horsley NSW 2530, SUPPORT
D, 16, Harry Potter, 04-01-1987, 568 Bong Bong St. Horsley NSW 2530, 20055, AVAILABLE
D, 17, James Bond, 02-14-1989, 7 Alan Bond St. Perth WA 6000, 20065, AVAILABLE
D, 18, Paris Hilton, 05-01-1977, 1 Hilton St. Melbourne VIC 3000, 10305, AVAILABLE
A, 19, Lady Gaga, 06-01-1992, 3 Pork st. Hobart TAS 7000, SUPPORT
D, 20, Robin Hood, 05-23-1999, 6 Nottingham Pl. Sydney NSW 2000, 10345, AVAILABLE
java algorithm readfile
1个回答
0
投票

像这样创建记录

public record Employee(String id, String name, String date, String address) { }

然后尝试这样的功能:

public List<Employee> getEmployeesFromFile(File file) {
    Scanner scanner;
    List<Employee> employees;
    if (file.exists()) {
        try {
            scanner = new Scanner(file);
            employees = new ArrayList<>();
            while (scanner.hasNextLine()) {
                String[] splitedNewLine = scanner.nextLine().split(",");
                employees.add(
                        new Employee(
                                splitedNewLine[0],
                                splitedNewLine[1],
                                splitedNewLine[2],
                                splitedNewLine[3]
                        )
                );
            }
            return employees;
        } catch (Exception e) {
            e.printStackTrace();
            return List.of();
        }

    } else {
        System.out.println("File not exists!!!");
        return List.of();
    }
}

这只是如何读取文件并将行映射到对象员工的示例。尝试根据您的问题和数据调整此代码。

© www.soinside.com 2019 - 2024. All rights reserved.