Search code examples
javafilecsvline

How to get a value in a file with coordinates in Java


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?


Solution

  • 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];
    }