Counting total file lines using BufferedReader [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • Number of lines in a file in Java

    17 answers



So basically, I'm trying to save several data saved from a file into an Array therefor I need to somehow get the size that the Array must be initialized to.



Is there any way to do that without creating another loop to count all the lines until the read line is null? Since that would require to re-open the file later which would not be very optimal. I'm trying to keep it as compact as possible.



Also, no I can't use ArrayLists.










share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:26










  • @HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
    – MoveUK
    Nov 10 at 1:30






  • 1




    Just so we know all the bizarre restrictions in advance, what else can't you use?
    – James K Polk
    Nov 10 at 1:30










  • Quite the opposite. It would give you enough memory when and where you need it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:30






  • 1




    Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
    – Hovercraft Full Of Eels
    Nov 10 at 1:36














up vote
0
down vote

favorite













This question already has an answer here:



  • Number of lines in a file in Java

    17 answers



So basically, I'm trying to save several data saved from a file into an Array therefor I need to somehow get the size that the Array must be initialized to.



Is there any way to do that without creating another loop to count all the lines until the read line is null? Since that would require to re-open the file later which would not be very optimal. I'm trying to keep it as compact as possible.



Also, no I can't use ArrayLists.










share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:26










  • @HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
    – MoveUK
    Nov 10 at 1:30






  • 1




    Just so we know all the bizarre restrictions in advance, what else can't you use?
    – James K Polk
    Nov 10 at 1:30










  • Quite the opposite. It would give you enough memory when and where you need it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:30






  • 1




    Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
    – Hovercraft Full Of Eels
    Nov 10 at 1:36












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Number of lines in a file in Java

    17 answers



So basically, I'm trying to save several data saved from a file into an Array therefor I need to somehow get the size that the Array must be initialized to.



Is there any way to do that without creating another loop to count all the lines until the read line is null? Since that would require to re-open the file later which would not be very optimal. I'm trying to keep it as compact as possible.



Also, no I can't use ArrayLists.










share|improve this question














This question already has an answer here:



  • Number of lines in a file in Java

    17 answers



So basically, I'm trying to save several data saved from a file into an Array therefor I need to somehow get the size that the Array must be initialized to.



Is there any way to do that without creating another loop to count all the lines until the read line is null? Since that would require to re-open the file later which would not be very optimal. I'm trying to keep it as compact as possible.



Also, no I can't use ArrayLists.





This question already has an answer here:



  • Number of lines in a file in Java

    17 answers







java arrays bufferedreader






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 1:24









MoveUK

31




31




marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 11 at 11:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2




    Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:26










  • @HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
    – MoveUK
    Nov 10 at 1:30






  • 1




    Just so we know all the bizarre restrictions in advance, what else can't you use?
    – James K Polk
    Nov 10 at 1:30










  • Quite the opposite. It would give you enough memory when and where you need it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:30






  • 1




    Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
    – Hovercraft Full Of Eels
    Nov 10 at 1:36












  • 2




    Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:26










  • @HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
    – MoveUK
    Nov 10 at 1:30






  • 1




    Just so we know all the bizarre restrictions in advance, what else can't you use?
    – James K Polk
    Nov 10 at 1:30










  • Quite the opposite. It would give you enough memory when and where you need it.
    – Hovercraft Full Of Eels
    Nov 10 at 1:30






  • 1




    Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
    – Hovercraft Full Of Eels
    Nov 10 at 1:36







2




2




Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
– Hovercraft Full Of Eels
Nov 10 at 1:26




Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it.
– Hovercraft Full Of Eels
Nov 10 at 1:26












@HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
– MoveUK
Nov 10 at 1:30




@HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice
– MoveUK
Nov 10 at 1:30




1




1




Just so we know all the bizarre restrictions in advance, what else can't you use?
– James K Polk
Nov 10 at 1:30




Just so we know all the bizarre restrictions in advance, what else can't you use?
– James K Polk
Nov 10 at 1:30












Quite the opposite. It would give you enough memory when and where you need it.
– Hovercraft Full Of Eels
Nov 10 at 1:30




Quite the opposite. It would give you enough memory when and where you need it.
– Hovercraft Full Of Eels
Nov 10 at 1:30




1




1




Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
– Hovercraft Full Of Eels
Nov 10 at 1:36




Otherwise you're talking about reading through the file twice, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file?
– Hovercraft Full Of Eels
Nov 10 at 1:36












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I would go for an ArrayList, as Haroon and Hovercraft.



However, if it is not an option, why not :



// 1. Create a new Set of String
Set<String> stringSet = new HashSet<String>();
// 2. Loop through bufferedReader and add lines to set
for (String line = br.readLine(); line != null; line = br.readLine())
stringSet.add(line);

// 3. Copy set to a new array
int n = stringSet.size();
String array = new String[n];
array = stringSet.toArray(array);


But don't expect huge performances.






share|improve this answer
















  • 2




    Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
    – Stephen C
    Nov 10 at 2:08











  • And if OP can't use a List they probably can't use a Set
    – GBlodgett
    Nov 10 at 2:08










  • Seems there are some good reason to not do that, indeed !
    – AllirionX
    Nov 10 at 18:02

















up vote
0
down vote













You can get the total number of lines by :



long count = Files.lines(Paths.get(PATH)).count();


Now you can create an array using count



String numOfLines = new String [(int) count];


The Files.lines(Path path) internally uses BufferedReader to read the file.






share|improve this answer





























    up vote
    -1
    down vote













    If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.



    If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop






    share|improve this answer



























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I would go for an ArrayList, as Haroon and Hovercraft.



      However, if it is not an option, why not :



      // 1. Create a new Set of String
      Set<String> stringSet = new HashSet<String>();
      // 2. Loop through bufferedReader and add lines to set
      for (String line = br.readLine(); line != null; line = br.readLine())
      stringSet.add(line);

      // 3. Copy set to a new array
      int n = stringSet.size();
      String array = new String[n];
      array = stringSet.toArray(array);


      But don't expect huge performances.






      share|improve this answer
















      • 2




        Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
        – Stephen C
        Nov 10 at 2:08











      • And if OP can't use a List they probably can't use a Set
        – GBlodgett
        Nov 10 at 2:08










      • Seems there are some good reason to not do that, indeed !
        – AllirionX
        Nov 10 at 18:02














      up vote
      0
      down vote













      I would go for an ArrayList, as Haroon and Hovercraft.



      However, if it is not an option, why not :



      // 1. Create a new Set of String
      Set<String> stringSet = new HashSet<String>();
      // 2. Loop through bufferedReader and add lines to set
      for (String line = br.readLine(); line != null; line = br.readLine())
      stringSet.add(line);

      // 3. Copy set to a new array
      int n = stringSet.size();
      String array = new String[n];
      array = stringSet.toArray(array);


      But don't expect huge performances.






      share|improve this answer
















      • 2




        Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
        – Stephen C
        Nov 10 at 2:08











      • And if OP can't use a List they probably can't use a Set
        – GBlodgett
        Nov 10 at 2:08










      • Seems there are some good reason to not do that, indeed !
        – AllirionX
        Nov 10 at 18:02












      up vote
      0
      down vote










      up vote
      0
      down vote









      I would go for an ArrayList, as Haroon and Hovercraft.



      However, if it is not an option, why not :



      // 1. Create a new Set of String
      Set<String> stringSet = new HashSet<String>();
      // 2. Loop through bufferedReader and add lines to set
      for (String line = br.readLine(); line != null; line = br.readLine())
      stringSet.add(line);

      // 3. Copy set to a new array
      int n = stringSet.size();
      String array = new String[n];
      array = stringSet.toArray(array);


      But don't expect huge performances.






      share|improve this answer












      I would go for an ArrayList, as Haroon and Hovercraft.



      However, if it is not an option, why not :



      // 1. Create a new Set of String
      Set<String> stringSet = new HashSet<String>();
      // 2. Loop through bufferedReader and add lines to set
      for (String line = br.readLine(); line != null; line = br.readLine())
      stringSet.add(line);

      // 3. Copy set to a new array
      int n = stringSet.size();
      String array = new String[n];
      array = stringSet.toArray(array);


      But don't expect huge performances.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 10 at 1:41









      AllirionX

      1765




      1765







      • 2




        Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
        – Stephen C
        Nov 10 at 2:08











      • And if OP can't use a List they probably can't use a Set
        – GBlodgett
        Nov 10 at 2:08










      • Seems there are some good reason to not do that, indeed !
        – AllirionX
        Nov 10 at 18:02












      • 2




        Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
        – Stephen C
        Nov 10 at 2:08











      • And if OP can't use a List they probably can't use a Set
        – GBlodgett
        Nov 10 at 2:08










      • Seems there are some good reason to not do that, indeed !
        – AllirionX
        Nov 10 at 18:02







      2




      2




      Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
      – Stephen C
      Nov 10 at 2:08





      Why not? 1) Because it will use a lot more memory than just using an ArrayList. 2) Because duplicate lines will be lost. 3) Because the lines will be reordered.
      – Stephen C
      Nov 10 at 2:08













      And if OP can't use a List they probably can't use a Set
      – GBlodgett
      Nov 10 at 2:08




      And if OP can't use a List they probably can't use a Set
      – GBlodgett
      Nov 10 at 2:08












      Seems there are some good reason to not do that, indeed !
      – AllirionX
      Nov 10 at 18:02




      Seems there are some good reason to not do that, indeed !
      – AllirionX
      Nov 10 at 18:02












      up vote
      0
      down vote













      You can get the total number of lines by :



      long count = Files.lines(Paths.get(PATH)).count();


      Now you can create an array using count



      String numOfLines = new String [(int) count];


      The Files.lines(Path path) internally uses BufferedReader to read the file.






      share|improve this answer


























        up vote
        0
        down vote













        You can get the total number of lines by :



        long count = Files.lines(Paths.get(PATH)).count();


        Now you can create an array using count



        String numOfLines = new String [(int) count];


        The Files.lines(Path path) internally uses BufferedReader to read the file.






        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          You can get the total number of lines by :



          long count = Files.lines(Paths.get(PATH)).count();


          Now you can create an array using count



          String numOfLines = new String [(int) count];


          The Files.lines(Path path) internally uses BufferedReader to read the file.






          share|improve this answer














          You can get the total number of lines by :



          long count = Files.lines(Paths.get(PATH)).count();


          Now you can create an array using count



          String numOfLines = new String [(int) count];


          The Files.lines(Path path) internally uses BufferedReader to read the file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 10:13

























          answered Nov 10 at 6:57









          Nicholas K

          4,20241029




          4,20241029




















              up vote
              -1
              down vote













              If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.



              If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop






              share|improve this answer
























                up vote
                -1
                down vote













                If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.



                If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop






                share|improve this answer






















                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.



                  If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop






                  share|improve this answer












                  If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.



                  If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 1:29









                  Haroon Iftikhar

                  1838




                  1838













                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20