简体   繁体   中英

how to skip first line when reading a csv in java

I want to skip the first col of first row and compare each element with string. Java Code

 String csvFileToRead = "C:\\Users\\Dell\\Downloads\\Tasks.csv";        
            BufferedReader br = null; 
            String[] Task = null;
            String line;  
              String splitBy = ",";
              br = new BufferedReader(new FileReader(csvFileToRead)); 
              System.out.println("Excel data for Subejct: ");
            while ((line = br.readLine()) != null) {            
                Task = line.split(splitBy); 
                System.out.println(Task[0]);    
               }  

To skip the first row just do a br.readLine() . To skip the first column start iterating the cells at 1 instead of 0 .

See this example:

br.readLine(); //skips the first row
while ((line = br.readLine()) != null) {
    String[] cells = line.split(splitBy);
    //starting from 1 essentially skips the first column
    for (int col = 1; col < cells.length; col++) {
        if (cells[col].equals(someString)) {
            //do something
        }
    }
}

Add a br.readLine() outise the loop, for the row jump. For printing all cols but the first one do:

for(int i = 1; i < Task.lenght ; ++i){
 System.out.println(Task[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM