1

I have a csv file:

something,something,something
hello,hello,hello
new,new,new

I want to split each line and save it into an array so I could access them. e.g. index[0] = something,something,something

I used index.split("\n") But it isn't working.

My code is:

public class RowRead {
    public static void main(String[] args) throws Throwable {
        CsvReader reader = new CsvReader("D://new.csv");
        while (reader.readRecord()) {
            String row = reader.getRawRecord();
            String lines[] = row.split("/n");
            System.out.println(lines[0]);
        }

        reader.close();
    }
}
User9123
  • 1,643
  • 1
  • 8
  • 21
Joe
  • 57
  • 1
  • 9
  • When I print the index 0 I get the whole csv file as it is. – Joe Jun 13 '21 at 17:40
  • I am using CsvReader jar file which I downloaded from sourcefeg.net Here's the link https://sourceforge.net/projects/javacsv/ – Joe Jun 13 '21 at 17:45
  • You using library for parsing csv for opening file only and later parsing it yourself using split? - it looks like nonsense. You can open file without additional csv library: https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java – User9123 Jun 13 '21 at 17:54
  • 1
    And you have wrong new line symbol, change it: row.split("/n") -> row.split("\n") – User9123 Jun 13 '21 at 17:55
  • I suggest [static] method [readAllLines](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readAllLines(java.nio.file.Path)) in class `java.nio.file.Files`. It gives you a `List` which you can convert to an array, if you like, but you can nevertheless access each line in the `List` via method `get(int)`, for example `get(0)` returns the first line in the CSV file. – Abra Jun 14 '21 at 06:43
  • I agree with @Abra. The CsvReader class is about dealing with CSV data. It seems you dont care at this point about the "structured" CSV data. If you really only want the lines to sit in an array or list, then use `Files.readAllLines()` . – GhostCat Jun 14 '21 at 08:57

1 Answers1

0

As I understand, you already get the next line with reader.getRawRecord() and no need to split it with multiple lines with \n. Just build a result array with given lines.

public static void main(String... args) throws IOException {
    String[] lines = readLinesFromCsv("D://new.csv");
    Arrays.stream(lines).forEach(System.out::println);

// something,something,something
// hello,hello,hello
// new,new,new
}

public static String[] readLinesFromCsv(String path) throws IOException {
    CsvReader reader = null;

    try {
        reader = new CsvReader(path);
        List<String> lines = new LinkedList<>();

        while (reader.readRecord()) {
            lines.add(reader.getRawRecord());
        }

        return lines.toArray(String[]::new);
    } finally {
        if (reader != null)
            reader.close();
    }
}

P.S. Thanks to @GhostCat. You do not need to use CsvReader at all and use out-of-box Files.readAllLines()

public static String[] readLinesFromCsv(String csv) throws IOException {
    List<String> lines = Files.readAllLines(Path.of(csv));
    return lines.toArray(String[]::new);
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • 1
    You should at least mention that using the CsvReader library like this is nonsensical. If you ONLY want to read the raw lines, then use `Files.readAllLines()` and be done?! – GhostCat Jun 14 '21 at 08:55