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?
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] 삭제
몇 마디 만하겠습니다