How to not read the header row while reading csv using Scanner?

Joseph Wong

I need to read a csv file in order to do some computations on it using java. I'm using Scanner to read it, but I want to start reading it after the first row (i.e., after the header row), right now I'm just doing: while(inputStream.hasNext()) to read all, what should I do so that the header row is not taken?

Voicu

You can do an extra scan.nextLine(); outside of the reading loop for which you don't use the return value:

Scanner scan = new Scanner(new File("C:\\input.txt"));
if (scan.hasNext()) {
    // skip header line
    scan.nextLine();
}

// write contents to output file
FileWriter fw = new FileWriter("C:\\output.txt");

// read the rest of the file
while (scan.hasNext()) {
    fw.write(scan.nextLine() + System.getProperty("line.separator"));
}

scan.close();
fw.close();

Edit: Added code for writing of the non-header content to an output file. Feel free to add some null checks on your scan and fw references if you want.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Reading text file in java using scanner

분류에서Dev

Error handling reading ints from Scanner in while loop

분류에서Dev

Error while reading csv file in R

분류에서Dev

Error while reading csv file in R

분류에서Dev

How can I skip even/odd rows while reading a csv file?

분류에서Dev

Can't read first row of CSV file

분류에서Dev

Read() method for InputText in PySimpleGUI only reading last row with variable layout

분류에서Dev

Bash: reading input within while read loop doesn't work

분류에서Dev

File read and write while reading the file line by line

분류에서Dev

Reading a csv file into csh using awk

분류에서Dev

How to make table with multi-tier row header (index) using Pandas

분류에서Dev

How to read the JQgrid row values

분류에서Dev

How to get excel row numbers to skip the header

분류에서Dev

Scanner not stopping from reading input

분류에서Dev

Delete row in excel while exporting using php

분류에서Dev

How to add a header to an existing csv file?

분류에서Dev

How can I read a CSV from Webserver using c# with LINQ? (ClickOnce deployment)

분류에서Dev

how to repeat loop based on user selection using scanner in java

분류에서Dev

do while loop with input scanner

분류에서Dev

Can't Find File When Using Scanner to read In a Previously Created File

분류에서Dev

How to find out if there is something to read before calling while read?

분류에서Dev

how to keep 10 biggest integer while reading a list in java?

분류에서Dev

How to pull day of month from Column header including row data?

분류에서Dev

How to skip cell value if row header contains certain text

분류에서Dev

How can I read data from CT10 bluetooth barcode scanner in Phonegap?

분류에서Dev

How to automatically read the header (variable names) from file in SAS

분류에서Dev

Pandas read_csv : header with two rows error로 데이터 가져 오기

분류에서Dev

Reading file with POSIX read

분류에서Dev

how to freeze table header using javascript

Related 관련 기사

  1. 1

    Reading text file in java using scanner

  2. 2

    Error handling reading ints from Scanner in while loop

  3. 3

    Error while reading csv file in R

  4. 4

    Error while reading csv file in R

  5. 5

    How can I skip even/odd rows while reading a csv file?

  6. 6

    Can't read first row of CSV file

  7. 7

    Read() method for InputText in PySimpleGUI only reading last row with variable layout

  8. 8

    Bash: reading input within while read loop doesn't work

  9. 9

    File read and write while reading the file line by line

  10. 10

    Reading a csv file into csh using awk

  11. 11

    How to make table with multi-tier row header (index) using Pandas

  12. 12

    How to read the JQgrid row values

  13. 13

    How to get excel row numbers to skip the header

  14. 14

    Scanner not stopping from reading input

  15. 15

    Delete row in excel while exporting using php

  16. 16

    How to add a header to an existing csv file?

  17. 17

    How can I read a CSV from Webserver using c# with LINQ? (ClickOnce deployment)

  18. 18

    how to repeat loop based on user selection using scanner in java

  19. 19

    do while loop with input scanner

  20. 20

    Can't Find File When Using Scanner to read In a Previously Created File

  21. 21

    How to find out if there is something to read before calling while read?

  22. 22

    how to keep 10 biggest integer while reading a list in java?

  23. 23

    How to pull day of month from Column header including row data?

  24. 24

    How to skip cell value if row header contains certain text

  25. 25

    How can I read data from CT10 bluetooth barcode scanner in Phonegap?

  26. 26

    How to automatically read the header (variable names) from file in SAS

  27. 27

    Pandas read_csv : header with two rows error로 데이터 가져 오기

  28. 28

    Reading file with POSIX read

  29. 29

    how to freeze table header using javascript

뜨겁다태그

보관