Reputation: 35
My programm needs to read a file that has different data structures with a variable separator. In my properties-file you can set the separator and put coordinates for values of different variables:
separator = ;
variable1 = 1,7
variable2 = 2,42
I would like to have a way where I can access a column and a line with some kind of coordinates.
I'm thinking of a syntax like this:
file.get(1,7,";")
(Which would give you the value of the 1st line and 7th column with the specific separator) Does someone know a library or a code snippet that does exactly this?
Upvotes: 0
Views: 319
Reputation: 4574
Using String.split()
:
public String get(File file, int lineNumber, int column, String separator ) {
//getting to the lineNumber of the file ommitted
// suppose you got it in a String named "line"
return line.split(separator)[column - 1];
}
Upvotes: 1
Reputation: 1478
Seems to be a simple file processing, You should first process the file -
ArrayList<ArrayList<String>>
processedFile "line".split(separator)
Once processedFile is ready, you can simply use processedFile.get(row).get(column)
. Also once the file is processed, all the other queries will be O(1). Hints are enough, try writing the code yourself, you will learn more.
PS: Take care of NullPointerExceptions
wherever required.
Upvotes: 0
Reputation: 20069
You can use OpenCSV or SuperCSV for example. I'm not aware of any library that does your 'coordinates' gettings, but it's as simple as reading the CSV with the given separator as List
-of-List
s and then call
csv.get(1).get(7)
Upvotes: 0