Skip first line while reading CSV file in Java
You might consider placing headerLine = br.readLine()
before your while loop so you consume the header separately from the rest of the file. Also you might consider using opencsv for csv parsing as it may simplify your logic.
How can I skip the first line of a csv in Java?
You may want to read the first line, before passing the reader to the CSVParser
:
static void processFile(final File file) {
FileReader filereader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(filereader);
bufferedReader.readLine();// try-catch omitted
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
CSVParser parser = new CSVParser(bufferedReader, format);
final List<CSVRecord> records = parser.getRecords();
//stuff
}
Skipping first line in csv using linereader.readline() but facing code quality issue
Rather than doing an inline .readLine()
to skip over an item, sonarqube is saying that you should still assign it to a variable.
"When a method is called that returns data read from some data source, that data should be stored rather than thrown away. Any other course of action is surely a bug." - Sonarqube docs
This is being done as arbitrarily throwing away data can be very dangerous on large scales where no one may be able to easily see the input/output of a specific file.
Basically rather than:bufferedReader.readline()
String line
//do whatever file read you're going to do thing
You should have:String line = bufferedReader.readline()
//Do whatever logic you're going to do with the file
This should satisfy Sonarqube.
Java Reading CSV file into array but ignore first line
Add a readLine()
before the while loop to skip the first line.
br = new BufferedReader(new FileReader(file));
br.readLine(); //read the first line and throw it away
while ((line = br.readLine()) != null) {
Skip first line using Open CSV reader
This constructor of CSVReader class will skip 1st line of the csv while reading the file.
CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
Related Topics
How to Mock a Rest Template Exchange
How to Connect Java Backend With Html/Css Frontend
Nb: Java_Home Should Point to a Jdk Not a Jre
Jpa- Joining Two Tables in Non-Entity Class
A Jsonobject Text Must Begin With '{' At 1 [Character 2 Line 1] With '{' Error
How to Display Pdf Inside Webview in Android
Checking for Null on a Collection in Jpql Queries
How to Tell a Mockito Mock Object to Return Something Different the Next Time It Is Called
How Would You Make a !!Say Command for a Discord Bot
Issue With Java 8 Collectors Type Mismatch: Cannot Convert from List<Object> to List<String>
How Many Times Will This Loop Execute
How to Call Methods Simultaneously in Java
Find Everything Between Two Xml Tags With Regex
Could Not Read Json: Can Not Deserialize Instance of Hello.Country[] Out of Start_Object Token
How to Write a User Input Based Constructor
Requestmethod Post Not Supported || Spring Login
Iterating Over Two Arrays Simultaneously Using for Each Loop in Java