Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading .tsv file in Java

Tags:

java

csv

I am a beginner and I am trying to read a .tsv data in Java and save the lines to an ArrayList. I wrote a method for it but the only thing I get is line id's and nothing more... I can't find an error. Could you please help me?

public static ArrayList<String[]> tsvr(File test2) throws IOException {
    BufferedReader TSVReader = new BufferedReader(new FileReader(test2));
    String line = TSVReader.readLine();
    ArrayList<String[]> Data = new ArrayList<>(); //initializing a new ArrayList out of String[]'s
    try {
        while (line != null) {
            String[] lineItems = line.split("\n"); //splitting the line and adding its items in String[]
            Data.add(lineItems); //adding the splitted line array to the ArrayList
            line = TSVReader.readLine();
        } TSVReader.close();
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }
    return Data;
}
like image 779
ta4le Avatar asked Dec 31 '22 03:12

ta4le


2 Answers

First, you should move readLine to a while, so that you can read all the lines of the file. Then you should split the line by tab \t because it is a tab separated file

public static ArrayList<String[]> tsvr(File test2) {
    ArrayList<String[]> Data = new ArrayList<>(); //initializing a new ArrayList out of String[]'s
    try (BufferedReader TSVReader = new BufferedReader(new FileReader(test2))) {
        String line = null;
        while ((line = TSVReader.readLine()) != null) {
            String[] lineItems = line.split("\t"); //splitting the line and adding its items in String[]
            Data.add(lineItems); //adding the splitted line array to the ArrayList
        }
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }
    return Data;
}

If you want to print the list of array:

Data.forEach(array -> System.out.println(Arrays.toString(array)));
like image 116
Germano Mosconi Avatar answered Jan 02 '23 16:01

Germano Mosconi


Explanation

  • You have to split on \t (tab) instead of \n (newline). As your lines are already single lines, not multiple.
  • Another issue in your code is that you close your stream manually, this is unsafe and will create a resource leak in exception-case. You may use try-with-resources to close it safely.

NIO

Instead of fixing your existing code which other answers already did, may I suggest a more compact and possibly more readable version that uses NIO (Java 8 or newer) which does the same:

return Files.lines(file.toPath())
    .map(line -> line.split("\t"))
    .collect(Collectors.toList());

If you can modernize your method parameters, I would suggest to make it Path path instead of File file, then you can simply do Files.lines(path)

like image 29
Zabuzard Avatar answered Jan 02 '23 17:01

Zabuzard