Richard Garcia Richard Garcia - 1 year ago 43
Java Question

what structured data objets should I use to manipulate data from a .txt?

first sorry about my bad english. I have a little problem, I need to read a .txt file that store the result of a SQL query.

the file .txt file has the following structure

GMM-1473879866 | 212349876543 | -150 | 14-SEP-16 PM | DebitBalance-[-150] | 212343214567 | 100 | 14-SEP-16 PM | Balance[150]

I mostly use PHP to process query results but right now I have to use java, is there any object, class, framework or library that could help me to store the data into an array, structure or so?

I want the data structure to look like this:

1 | GMM-1473879866 | 212349876543 | -350 | 14-SEP-16 PM | Balance-[-150] | 212343214567 | 100 | 14-SEP-16 PM | Balance[150]
2 | GMM-1812568432 | 212345634411 | -150 | 14-SEP-16 PM | Balance-[-150] | 212349081123 | 0 | 14-SEP-16 PM | Balance[150]

being 1..2...n some kind of array "key".

this data structure (ie. bidimensional array) will be used to send some parameter to an API, the API need to be executed manually for every query row (usually 20/day) so this little software aim to automatize this process.

I can not modify or view the query code, so I need to work with this file, the program will be running automatically everyday.

Answer Source

You can use a string split to parse each of the lines. Each line contains 9 groups separated by a pipe character which is surrounded by a variable amount of white-space. So your split regex looks like "\s*|\s*". You then have to escape some characters to work in java... so you get "\\s*\\|\\s*"

final List<String[]> rows = new ArrayList<>();
try (final BufferedReader reader= new BufferedReader(new FileReader("sqlqueuery.txt")))
    String line;
    while ((line = reader.readLine()) != null) {

} catch (final IOException e) {
    //TODO handle errors

In terms of what data structure to use, you could just create a custom class with named fields for each on the individual cells.