-1

I am working on a Java project where I need to handle CSV files. Specifically, I need to read and write CSV files and process the data into arrays for further manipulation. I have researched different libraries and approaches, but I am unsure about the best option and how to correctly implement it, especially when it comes to efficiently storing the CSV data into arrays.

I have explored various libraries and approaches for handling CSV files in Java, but I haven't been able to determine the most efficient and effective solution for reading, writing, and processing CSV data into arrays. I have attempted some implementations, but I'm not getting the desired results or struggling with performance issues.

I would appreciate it if someone could provide me with a simple example and explain the recommended approach to efficiently read, write, and process CSV files into arrays in Java.

  • If your needs are very simple (i.e., you control exactly what goes into the file and you don't need to care about quote, escapes, etc.) then reading a line and splitting on comma does the job with least fuss. No need to use a complicated swiss army knife when a single flat-blade screwdriver does the job! – Arfur Narf Jul 08 '23 at 23:43

2 Answers2

1

Welcome to stack overflow!

The guidelines say that questions asking for recommendations for libraries and questions asking for opinions are inappropriate. So your question may get closed for either or both of those reasons.

However, I'll attempt to answer your question, with the caveat that what follows is not necessarily the "best" or "most efficient" way to read CSV data, especially since it reads all of the CSV data into memory in one shot, and could cause out-of-memory problems if you try to read a file that is bigger than available (to your application) memory....YMMV

I use the "opencsv" library

For example,

CSVReader csvReader = new CSVReader(new FileReader(inputFilename));
List<String[]> data = csvReader.readAll();

Each item in the list is a row, and each item the row's String[] is a column value for that row.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

It depends on your data.

A CSV can be trivial if your data may contain commas.

You could implement a TSV, which uses the tab character.

Wikipedia – Delimiter-separated values.

"... I would appreciate it if someone could provide me with a simple example and explain the recommended approach to efficiently read, write, and process CSV files into arrays in Java."

Here is a basic read and write, using a TSV structure.

void append(String[] strings, File file) throws IOException {
    try (FileWriter writer = new FileWriter(file, true)) {
        writer.write(String.join("\t", strings));
        writer.write(System.lineSeparator());
    }
}

List<String[]> read(File file) throws IOException {
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        List<String[]> list = new ArrayList<>();
        String line;
        while ((line = reader.readLine()) != null)
            list.add(line.split("\t", -1));
        return list;
    }
}

And, here is a usage.

File file = new File("files/data.tsv");
append(new String[] { "a", "b", "c" }, file);
append(new String[] { "d", "e", "f" }, file);
List<String[]> list = read(file);
for (String[] strings : list)
    System.out.println(Arrays.toString(strings));

The contents of data.tsv,

a   b   c
d   e   f

Output

[a, b, c]
[d, e, f]
Reilas
  • 3,297
  • 2
  • 4
  • 17