Java - text file analysis

I have a text input file in this format:

<target1> : <dep1> <dep2> ... <target2> : <dep1> <dep2> ... ... 

And a method that takes two parameters

 function(target, dep); 

I need this parsing to call my method with every goal, and for example:

 function(target1, dep1); function(target1, dep2); function(target1, ...); function(target2, dep1); function(target2, dep2); function(target2, ...); 

What would be the most efficient way to call function(target,dep) on each line of a text file? I tried to spoof the scanner and string.split, but was unsuccessful. I'm at a dead end.

Thanks.

+4
source share
2 answers
  • Read a string in String myLine
  • split myLine to : in String[] array1
  • split array1[1] on ' ' to String[] array2
  • Go through array2 and call function(array1[0], array2[i])

So...

 FileReader input = new FileReader("myFile"); BufferedReader bufRead = new BufferedReader(input); String myLine = null; while ( (myLine = bufRead.readLine()) != null) { String[] array1 = myLine.split(":"); // check to make sure you have valid data String[] array2 = array1[1].split(" "); for (int i = 0; i < array2.length; i++) function(array1[0], array2[i]); } 
+8
source

Firstly, you need to read the line from the file and after that reading separator line, so your code should look like this:

 FileInputStream fstream = new FileInputStream("your file name"); // or using Scaner DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // split string and call your function } 
+1
source

All Articles