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.

Similar Messages

  • 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)

  • 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.

  • 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?

  • (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.

  • 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.

  • Counting number of lines in a text/csv file using xquery/xslt.

    Hi,
    I have a CSV file, I need to count the total number of lines in that file. This I have to use in OSB. My requirement is I have to count total no of lines in the $body file (CSV/flat file) and subtract header and footer lines from it.
    EX:
    header,1, @total_no_of_detal@
    detail,1
    detail,2
    detail,3
    detail,n
    footer, 1
    If suppose i have 10 detail lines, and I am getting body of the file as shown above,
    then in the final file, I have to change the body of the file as:
    header,1, *10*
    detail,1
    detail,2
    detail,3
    detail,n
    footer, 1
    Please advice how to do this in OSB.
    Edited by: user12679330 on Aug 2, 2011 2:34 AM

    I would suggest you to use MFL to convert the file into XML when you read it and then you can count the detail elements within the XML and update the values in Header element before writing the file again in flat file format again using MFL.
    You can read the documentation of Format Builder utility which is used to define MFL here:
    http://download.oracle.com/docs/cd/E21764_01/doc.1111/e15866/part_fb.htm
    Alternatively, another approach is the read the whole flat file as a text string and then doing needed manipulations to get what you need. If you want to do it like this then you will need to consider following things:
    1. Is the file in DOS format or Unix format? (To know the End of line character in use)
    2. Does the file contain an end of line at the end of file? (whether there is an end of line char at the end of data of footer record)
    Once you know above, you can try an xquery like following:
    let $intermediateXML :=
    <Root>
    for $a in (tokenize($in, 'EOL'))
    return
    if (string-length($a) > 0)
    then
    <record>{$a}</record>
    else ()
    </Root>
    let $count := count($intermediateXML/record)
    let $outXML :=
    <OutRoot>
    for $i in 1 to $count
    return
    if (data($i) = 1)
    then
    <Record>{concat($out, $intermediateXML/record[$i]/text(), ',Count=',($count)-2)}</Record>
    else
    <Record>{concat($out, $intermediateXML/record[$i]/text())}</Record>
    </OutRoot>
    return op:concatenate($outXML/Record/text(), 'EOL')In Above XQuery, replace EOL with the end of line character which for unix wound be "&#xA ;" (remove quotes and space between the A and ; ) and for DOS would be "&#xD ;&#xA ;" (remove quotes and space between the A and ; and D ; )

  • 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

  • How can I find out the number of lines in a text file?

    How can I find out the number of lines in a text file?

    java.io.BufferedReader in = new java.io.BufferedReader( new java.io.FileReader( "YourFile.txt" ) );
    int lineCount = 0;
    while( in.readLine() != null )
    lineCount ++;
    System.out.println( "Line Count = " + lineCount );

  • How to find number of lines in an input file.

    Hi,
    Can someone tell me, if there is a way to find out the number of lines in an input file. I do not want to read line by line and then count the number of lines read.

    Then how do you think a program could determine the amount of lines in an input file? How does a human know how many lines are on a page? You're going to have to count the items that separate two lines to know how many lines are in your file.
    Either read line by line and count the amount or read char by char and count the number of '\n' occurences instead.
    If you have control over writing the input files, however, you can make this better. Write a header in the file that specifies the amount of lines in it. The first line of your file will then hold a number that represents the amount of lines in the file, so you need only read the first line to know how many lines are in your file.

  • How to resrict number of line items in IDOC  Payment file

    Hai
    I had an issue where user wants to restrict the number of line items in the payment IDOC file. Is there any possibilty to restict these number of line items to 6.
    Because the user has some reporting problem with the bank regarding the IDOC file
    Kindly help me
    Thanks in advance
    Regards,
    Akash Narayana

    Hi Akash,
    Execute transaction code <b>WE20</b> and open up the "<b>Partner Type B</b> - i.e. <b>Bank</b>". Select the bank in question. To the right of the screen, you would see 3 different sections. You are interested in the middle section "<b>Outbound parmtrs</b>.". Highlight the "<b>Message type</b>" (e.g. COSMAS - Master cost center) you want to reduce the size and click on the "<b>Display</b>" icon just underneath it. In the "<b>Outbound Options</b>" tab, change the "<b>PacketSize</b>" to 6 and save your entries.
    Repeat this for all other outbound parameter "<b>Message Types</b>" for this bank. e.g. "<b>EUPEXR</b> - Reference message for electr. signature (for ext. payments)" and "<b>PAYEXT</b> - Extended payment order" etc. That is, change the "<b>PacketSize</b>" for each to <b>6</b> and save your entires. This would reduce the number of each set of messages sent to the bank to 6.
    I hope the above helps.
    Do not forget to award the points please.
    Regards,
    Jacob

  • File splitter by number of lines

    hey
    i have a text file with 10000 records. i need to read the file and pass parameters which is the number of records. so eventually new files have to be created with that number of records inside each file.
    pls help me out in doin this.
    PraDz

    let me explain in brief
    i have a file name test.txt [10000 records]
    a. have to read this file
    b. specify the number_of_lines to be written
    c. create a new file and write the number_of_lines
    d. so that much number of files will be created.
    number_of_lines = 100
    new1.txt - 100 records
    new2.txt - 100 records
    new3.txt - 100 records
    new4.txt - 100 records
    PraDzLooks like you've got an algorithm ready to go. Now all you have to do is:
    1) Learn how to access command line arguments in your Java program.
    2) Learn how to open a file and read lines from it.
    3) Learn how to open a file and write lines to it.
    4) Learn how to count.
    And you're done! Easy.

  • Please how can i read file, to find number of lines and to extract number of block data, and number of data in each block

    please if you can give me an example

    I attach a file wich i want read:
    1- to know a number of lines in file
    2- from the second line of the file to the line befor NCOUNT, put the lines in array like this, Variable and values like this Ex: TIMET 1 0 1 ..... first line
    and put the values of NCOUNT in array
    I attach a file
    Attachments:
    karim1.txt ‏2 KB
    Read_Filenew.vi ‏29 KB

Maybe you are looking for

  • How to change the display view of the records and modified into a row

    I have a SQL which can count the condition(Bad,Poor,Worse) in a day and group it into a month and displays as follows: YEARS MONTHS LV    COUNTVAL 2009 Apr    Bad          5 2009 Apr    Poor         3 2009 Apr    Worse        2 2009 Aug    Bad       

  • Problem using Bridge CS5

    I have a photoshop CS5 and it works fine except Bridge CS5. It works fine on my Mac OSX 10.5.8 but it does not work on my new Mac OS X 10.8.2. When I try to open Bridge from photoshop CS5, error messeage appears and it says " Error2: photoshop is und

  • Airplay defaulting to dock connector

    hi, I have had intermittent problems using the BBC iPlayer app over the last two days. When using the app to play video, the sound will stop (silence), and the iPad volume control does not work, or show on screen indication. I have found that the Air

  • New Iphone4 Shows Preview of Texts and Chats

    Everytime I get a text or chat, it shows on the screen as a preview. I have the passcode on and have turned off the notifications. Is there a setting or an app to download that prevents them from automatically showing up or a way to block them so I c

  • Incredible 2 simply shuts off!!!!!!  Wondering?

    A few days ago my INcredible 2 simply started shutting off for no reason.  I would power it back up and it would do it again!  I started trying to figure it out and may have stumbled onto a issue.  It seems that the battery isn't holding a charge.  I