Read one line of a csv file in Java

后端 未结 6 2004
Happy的楠姐
Happy的楠姐 2021-01-21 16:59

I have a csv file that currently has 20 lines of data. The data contains employee info and is in the following format:

first name, last name, Employee ID

So one

6条回答
  •  滥情空心
    2021-01-21 17:40

    You can read text from a file one line at a time and then do whatever you want to with that line, print it, compare it, etc...

    // Construct a BufferedReader object from the input file
    BufferedReader r = new BufferedReader(new FileReader("employeeData.txt"));
    int i = 1;
    try {
    
        // "Prime" the while loop        
        String line = r.readLine();
        while (line != null) {
    
            // Print a single line of input file to console
            System.out.print("Line "+i+": "+line); 
    
            // Prepare for next loop iteration
            line = r.readLine();
            i++;
        }
    } finally {
        // Free up file descriptor resources
        r.close();
    }
    
    // Remember the next available employee number in a one-up scheme
    int nextEmployeeId = i;
    

提交回复
热议问题