Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

read file to list and store in array

I had a file I need to read it and store it in array...
I am done with it...

but i found a problem and now i want to read the file into a list and then store it into array...

below is the code that i had written with out reading it into list...

a friend of this forum gave me the below link...

http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line-into-a-list

so can any one tell me how to apply the above concept in above link to my code...


class ReadFile
  {     
        int kMaxLines = 60000;          
        double[][] valuePairs = new double[2][kMaxLines];
        public void fileRead()
        {
                try             
                {       
                        int kMaxLines = 60000;  
                        FileInputStream fstream = new FileInputStream("/output.txt");
                        DataInputStream datastream = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
                        String strLine;
                        int line = 0;
                        while ((strLine = br.readLine()) != null)
                                {
                                String temp_strs[] = strLine.split("\t");
                                for (int j = 0; j < 2; j++)
                                        valuePairs[j][line] = Double.parseDouble(temp_strs[j]);
                                line++;
                        }
                                datastream.close();
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aman123_123
aman123_123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

double[][] valuePairs = new double[kMaxLines][2];

instead of above format can it be done as shown in my format...
i.e.,

double[][] valuePairs = new double[2][kMaxLines];
what r u trying to do with that code?

while ((strLine = br.readLine()) != null)
                                {
                                String temp_strs[] = strLine.split("\t");
for (int j = 0; j < 2; j++)
                                        valuePairs[j][line] = Double.parseDouble(temp_strs[j]);

the above line just overrites the exisiting!
Avatar of aman123_123
aman123_123

Yes it can be done in that way, but it is easier to understand using the way I have done.