Link to home
Start Free TrialLog in
Avatar of puneet kumar
puneet kumar

asked on

want to read .txt file need to calculate total no of lines and save each line in string type of array.

want to read a file line by line , each line want to store in string array and want also total no of lines

BufferedReader input = null;
            int numLines = 0;
            String[] lines = null;
            try {

                  input = new BufferedReader(new FileReader(lFilePathStr));
                  try {
                        String line = null;

                        while ((line = input.readLine()) != null) {
                              numLines++;
                        }
                        for(int count = 0; count < numLines; count++) {
                              
                              lines[count] = input.readLine();
                    }
                  } catch(Exception e) {
                        e.printStackTrace();
                  }
            } catch (IOException ex) {
                  ex.printStackTrace();
            }
            
            
            
      in below line only 1st line read after that it will come in catch block please let me know where i am wrong.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't read a BufferedReader twice without resetting it. Why do you think you need an array and not a List?
Avatar of puneet kumar
puneet kumar

ASKER

no you have any better idea please explain me and give me some code snippet.i don't want to read file 2 times 1st for calculating total no of lines other is storing line by line data i want both of the things has been completed read a file only once .
There is no "easy" way in java to dynamically expand the array as you add each line, and you don't know how many lines there will be before you read the file.

A better choice for storing the lines as you read the file would be an ArrayList, which does support an Add method to add new elements to the list.


»bp
See the following
Never read a file twice unless absolutely forced to

http://technojeeves.com/index.php/aliasjava1/81-reading-a-file-into-a-list-of-string-in-java-7-and-above
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

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
No need to copy elements - Lists already have a toArray method:
https://docs.oracle.com/javase/9/docs/api/java/util/ArrayList.html#toArray-T:A-
If you were talking to me, yes, understood; but in quoting a String[] I was being specific and apparent about what the OP wanted. The lines would need to be iterated anyway as proof in any case, so to me it's more explicit for demonstration purposes at least.
Yes
'Footnote :'

It's a bit Old School, but a Vector might be a considerable option too, since it can effectively be seen as a String[] (via decl as a 'Vector<String>'), and it comes without having to worry about knowing the size of the required [] in advance, and can be used to determine the number of lines that were read in from the file using its '.size()' method.
But the same goes for ArrayList and it doesn't have the overhead of synchronization
Absolutely.
Just another angle really.
Thanx a ton.
SOLUTION
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
Thanx a ton for support