Skip to content

Instantly share code, notes, and snippets.

@vernetto
Last active November 23, 2023 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernetto/be338d19faa0021cb9defecf23651490 to your computer and use it in GitHub Desktop.
Save vernetto/be338d19faa0021cb9defecf23651490 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVProcessor {
public static void main(String[] args) {
String filePath = "path/to/your/csvfile.csv"; // Replace with your file path
processCSV(filePath);
}
public static void processCSV(String filePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
StringBuilder accumulatedLine = new StringBuilder();
while ((line = reader.readLine()) != null) {
accumulatedLine.append(line);
// Check if line ends with a comma or is the last line
if (!line.endsWith(",")) {
// If it doesn't end with a comma, read the next line
continue;
}
// Process the complete line
processLine(accumulatedLine.toString());
// Reset the StringBuilder for the next line
accumulatedLine.setLength(0);
}
// Process the last accumulated line if it's not empty
if (accumulatedLine.length() > 0) {
processLine(accumulatedLine.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void processLine(String line) {
// Implement your line processing logic here
System.out.println(line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment