Determining number of lines in a File without loading it to memory

hello guys,
I'm just wondering if there is a way to know the number of lines in a file without having to load the file entirely into memory and count the lines.
in my project i have a restriction that says that i'm not allowed to load into memory more than 10 KB. i have to read the file block by block, say ten lines at a time.
I actually did the second point which is reading the file 10 lines at a time, but still the total number of lines is still hard coded.
thank you for your help !
I

ralfph wrote:
hello guys,
I'm just wondering if there is a way to know the number of lines in a file without having to load the file entirely into memory and count the lines.
in my project i have a restriction that says that i'm not allowed to load into memory more than 10 KB. i have to read the file block by block, say ten lines at a time.
I actually did the second point which is reading the file 10 lines at a time, but still the total number of lines is still hard coded.
thank you for your help !
ISo just read a line at a time, increase a counter, and don't save the line. As long as no one line is longer than 10 kB, you're fine.
If it's possible for a line to be longer than 10 kB, then you'll have to read into a byte[] or char[] and iterate over it looking for end of line characters.

Similar Messages

  • Read a file without loading it into memory

    I have a VERY large file filled with words. I want to determine if a String in my program matches one of them. Loading the entire file into memory would take a long time and would use way too much memory. Is there some way to load just the part I need each time or make the comparison directly from the file on the hard disk?

    The file is static, so your plan seems to have merit.
    I've only used sequential files before, but I'm
    assuming this would require a random access file since
    you have to begin reading at a specified offset. Am I
    correct?Yes.
    In addition, if this file is fixed-length "records" so that you could programmatically determine the word offsets without having to keep a map of indexes in memory, you could just employ a search algorithm (I forget what this one is called) - you could start by defining 2 endpoints, initially at offset 0 and the offset of the last word.
    Iteratively read the word halfway between the 2 endpoints, adjusting the endpoints until a match is found or the endpoints converge.

  • How to count number of lines in a file?

    I am using a BufferedReader to read my file, but I like to know how to count the number of lines in a file is there a way to do that in Buffered Reader or do I have to use FileInputLineStream?

    Well, if you know how to read one line with a Buffered reader, and you know how to keep reading lines until you run out, then all you have to to is initialize a counter to zero before you start reading, add 1 to it each time you read a line, and then examine its value when you're done.
    Try that, and post again if you get stuck.
    Just be careful how you construct your loop. "Off by one" errors are a common way to get bitten. It could happen if you count what you think is the last line, but you've actually already counted all the lines and you end up counting one extra for "there are no more lines".

  • How to get the number of lines of a file?

    Folks:
    Is there a way to get the number of lines of a text file? I don't know if there is an existing method to do that.
    Thanks a lot.

    HI
    I found some solution
    other than increment loop and all
    here is the code it might helpful to u
    //returns the number of lines in a file
    //author : Ravindra S
    //Symphony software Hyderabad
    try
    RandomAccessFile randFile = new RandomAccessFile(csvFile,"r");
    long lastRec=randFile.length();
    randFile.close();
    FileReader fileRead = new FileReader(csvFile);
    LineNumberReader lineRead = new LineNumberReader(fileRead);
    lineRead.skip(lastRec);
    countRec=lineRead.getLineNumber()-1;
    fileRead.close();
    lineRead.close();
    catch(IOException e)

  • Count the number of lines in  each file in a directory

    Hello,
    I would like to count the number of lines for each file in my directory and subdirectories.
    I wonder how can I count the lines of the specified files?
    Here is the program which is listed the files in the specified directory:
                        String path = "C:/User/Studies/Pracices/src/Pract1";
           String files;
           File folder = new File(path);
           File[] listOfFiles = folder.listFiles();
           for (int i = 0; i < listOfFiles.length; i++)
            if (listOfFiles.isFile())
         files = listOfFiles[i].getName();
         System.out.println(files);

    Ok so one more question.
    Actually my path is pointing to one Directory and like here I can just read from one file that the name of the file should be specified in the path is it possible to help me in that part?
    File f = new File("C:/User/Studies/Practices/src/Pract1/");
    FileReader fr = new FileReader(f);
          BufferedReader br = new BufferedReader(fr);
          String str = br.readLine();
              LineNumberReader ln = new LineNumberReader(br);
              int count = 0;
              while (ln.readLine()!=null)
                  count++;
              System.out.println("no. of lines in the file = " + count);

  • How do you find the number of lines in a file?

    I need to count how many lines there are in a file, I am using a BufferedReader to read in the data, but how can I find the number of lines?
    Thanks

    That depends. How do you define a line? Is it a specific number of
    characters, a String that's terminated by a newline character or some
    combination thereof?

  • Number of lines in a file

    Is there a method that returns the number of lines in a text file? I'm trying to make an array of arrays, with each inner array holding 4 strings from 4 consecutive lines from a file, and the outer array holding every one of those arrays that are possible in the file. I can't anticipate the amount of 4-line blocks im going to have tho, so i need something that returns the number of lines available and divides that by four. I'll then populate the outer array by looping that many times. If any of that made sense...

    Is there a method that returns the number of lines in
    a text file? I'm trying to make an array of arrays,
    with each inner array holding 4 strings from 4
    consecutive lines from a file, and the outer array
    holding every one of those arrays that are possible
    in the file. I can't anticipate the amount of 4-line
    blocks im going to have tho, so i need something that
    returns the number of lines available and divides
    that by four. I'll then populate the outer array by
    looping that many times. If any of that made sense...You would do better to use java.util.List implementations such as ArrayList and LinkedList then you don't need to know the number of lines.

  • Script to determine number of lines of text in a paragraph?

    I'm revisiting an issue I first posted here last summer, thanks to all who offered help, but I've not been able to sort it out.
    http://forums.adobe.com/thread/455526?tstart=0
    What I'm after is  a 'search function' to insert into a FindChangeByList.
    Jongware suggested the following - but when inserted into my list I can't get it to do anything - it doesn't throw up a Javascript error.
    app.findTextPreferences = NothingEnum.nothing;
    app.findTextPreferences.appliedParagraphStyle = "Callout_3";
    foundItems = app.activeDocument.findText();
    for (var i=foundItems.length-1; i>=0; i--)
      if (foundItems[i].paragraphs[0].lines == 2)
        foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_2";
      if (foundItems[i].paragraphs[0].lines == 1)
        foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_1";
    I regularly receive a 'word' document on which I run a 'FindChangeByList' which cleans up and formats the text with a number of GREP/text searches.
    One of the search lines converts a particluar string of text to a paragraph style "Callout_3" (the most common occurence of Callouts).
    This style applies a paragraph rule, above and below the line, to create a coloured block/band out of which said text is reveresed.
    The problem that I have is that the style (because of the paragraph rules), only works correctly if the paragraph has x3 lines of text.
    I have styles set up for every other eventuality - Callout_1 (for single line of text), Callout_2 (for x2 lines of text) etc.
    What I'm looking for is a script that can count the number of lines in the paragraph (once the "Callout_3" style has been applied) and then change the style accordingly to the number of lines in each paragraph/Callout.
    Any further thoughts?
    Steve

    Hi Steve!
    Well, I said it was untested ... Found two major errors. First one is also visible when you manually search for the style "Callout_3". Rather than marking each and every paragraph on its own, InDesign gathers as much as possible. Hence, just testing paragraph[0] would miss out all other paragraphs. Explicitly looping over all paragraphs per "found item" does work.
    The other one is sort of a "syntax" error. Merely testing "lines == 2" does not work -- but it doesn't give an error either! Apparently, it's okay to compare an object ("lines") to a number, even though it doesn't work. The property to check is "lines.length" -- that returns the number of lines for a paragraph.
    Putting it all together results in this script.
    'And now I shall test it,' said Wen. This time he twisted it gently to and fro. 'That si-si-si That simple-ple, eh eheh simple, eh?'
    ... And I tested it.
    app.findTextPreferences = NothingEnum.nothing;
    app.findTextPreferences.appliedParagraphStyle = "Callout_3";
    foundItems = app.activeDocument.findText();
    for (var i=foundItems.length-1; i>=0; i--)
    for (var j=0; j<foundItems[i].paragraphs.length; j++)
      if (foundItems[i].paragraphs[j].lines.length == 2)
       foundItems[i].paragraphs[j].appliedParagraphStyle = "Callout_2";
      if (foundItems[i].paragraphs[j].lines.length == 1)
       foundItems[i].paragraphs[j].appliedParagraphStyle = "Callout_1";

  • Can i load transaction data thro flat files without loading masters?

    Hi all,
    is it possible to load history data using flat files in sales cube without having loaded any masters?
    thanks

    Hi
    You can load the transaction data without loading the master data. You have to check the option in update rules (update also if no master data exists).
    In reporting you will not be able to see the attribute and text data. Its always adivceable to load master data first and run apply hierarchy/attribute change and load the transaction data.
    AHP: Nice to see you after a long time
    REGards
    Rak

  • How to find out memory consumption for table in HANA without load it into memory

    Hi,
    To determine the memory consumption for a table in HANA, you can query table M_CS_TABLES, however, it requires load table into memory first, I just wonder if there has another table store memory consumption information for all HANA tables regardless it load into memory or not. Below is screenshot for one of table in my system, since that table is partially loaded into memory, "Total Memory Consumption (KB):" tell me the memory consumption of the portion load into memory, what I am looking for is something like "Estimated Maximun Memory Consumption (KB)" which provides me total memory consumption for that table including portion doesn't load into memory, of course I can use this Esitmated information, but consider I have close to thousand tables in my HANA system already, it's not pratical to check tables one by one.
    Thanks,
    Xiaogang.

    Hi Xiaogang,
    Estimated Memory Size that you see in the Table Run time Information - same is available in M_CS_TABLES also
    If you don't get the size of any Table in M_CS_TABLES View, then the same will also not be available in Runtime information of the Table
    Even if tables are not loaded into memory, you can get the Estimated Size, just try running the query with filter LOADED = 'NO'
    Regards,
    Vivek

  • How to get total number of pages from .doc file without using Office interop?

    Hi,
    Kindly help me in getting the total number of pages from a .doc file not .docx file using C#. I know how to get by using Office interop but I do not want to use Office interop.
    So, without office automation in C# let me know how to get the total number of pages from a .doc file.
    Regards,
    Ashok

    Hi Ashok,
    >> I know how to get by using Office interop but I do not want to use Office interop
    Could you tell us why you don't want to use Office interop?
    As far as I know, this is the easiest way to achieve.Hmmm,this is my answer
    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.pagenumbers.startingnumber(v=office.14).aspx
    Best regards,
    Kristin
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • (SOLVED) How to automagically add X number of line to new file in vim?

    Basicly I want to add X number of predefined lines of text to every new file I create in vim; if I create file.cpp I want some text, if I create file.sh I want another text.
    I'm sure this is do-able?
    Last edited by dmz (2009-07-07 17:51:41)

    Brilliant! Thanks.

  • Determine number of lines of multiline container in ccBPM

    Hi guys,
    during my process a loop over a multiline element and send each entry to SAP backend.
    After shipment of the message the process waits for some time, to prevent problems caused by parallel messages in the backend.
    At the moment I use a block in the "for each" mode. But with this solution the wait step is executed unnecessary, after sending the last message of the multiline container variable.
    Is there a possibility to check the number of entries and solve this issue with a normal loop step and preventing the process for unnecessary wait step?
    I search SDN for some information relating this issue, but without success.
    Any help appreciated, thanks in advance
    Kind regards
    Jochen

    Hi Shabarish,
    thanks for helping.
    You are right I want to know how many entries are in the multiline container.
    I also thought about an export parameter in the transformation step where the multiline message is created.
    In this parameter I could return the number of entries.
    Maybe this would be an adequate approach for this issue.
    Unfortunately I wasn´t able to evaluate such a export parameter, when I tried it last time.
    I mentioned the issue in this [thread|;.
    maybe you know how to solve this issue..?
    kind regards
    Jochen

  • How to determine the lines in a file?

    Hi,
    I have a VI that reads the lines in a file and then I am using a for loop to analyze the data.
    Is there a way to determine the number of lines in the file so that I can feed it in the N value of the for loop?
    I tried the command EOF in the read lines VI with no success.
    Let me know,
    Fede
    Attachments:
    file reader CAN v2.5.vi ‏114 KB

    I think I understand what you are saying.
    You think I should reorgabize the array witout using the shift registers and using the read from spreadsheet vi.
    I am not saving the same file.
    I am trying to save the data in 3 different formats. I am eliminating the time stamp and saving it in order to use later on in my graphs. Then I am saving the raw data before I concatenate the first 4 bits and after I concatenate the the first 4 bits.
    If I use autoindexing I will not need the shift registers,right? Can I then save the while the program is running or I have to save at the end of the process.
    I am familiar with operations with strings but I am still a bit weak on array and string working toghether.
    I attach what I am doing.
    Thanks
    Attachments:
    sensorlog1.txt ‏1 KB
    canwithtime.txt ‏1 KB
    log1.txt ‏1 KB

  • Moving and Organizing files based on number of lines

    Hello,
    I have a directory containing nearly 1500 files, some of which are composed of 12 lines, some of which are composed of 4 lines, all of which are .phy (PHYLIP) files, which can be viewed within notepad/wordpad like text files.
    I need to run a script which will identify the number of lines in each file, and move them to the corresponding folder. File size will not work for sorting, I have already tried.
    i.e.- all files with 4 lines in one folder, all files with 12 lines in another folder, no files left (so i can be sure everything was inspected) in the main directory.
    Can anyone help?
    thanks
    originally posted in the microsoft answers forum and redirected here by a mod.

    I would suggest you to post your question in scripting forums : http://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG
    Arnav Sharma | http://arnavsharma.net/ Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading
    the thread.

Maybe you are looking for