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;
}
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)));
\t
(tab) instead of \n
(newline). As your line
s are already single lines, not multiple.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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With