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.

Similar Messages

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

  • 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

  • Number of lines being written into a file

    Hi All,
    I get "n" number of input text files - having "m" number of lines each.
    Based on the first character of each line (there are 5 distinct types I can get) i have to write each line in a separate file.
    To reduce I/O i am first storing the lines in "m" different StringBuffer objects (based on the first character of each line).
    Then finally I am storing each stringbuffer content (using stringbuffer.toString()) in a file... So finally I will have "m" different output files.
    Now I have a small constraint. The number of lines I can write to any output file cannot exceed 500. If it does, I have to create a new file with the same name but with an extension like "fileOne_1" and then save lines 501-1000 in the new file.
    How do I achieve this? Is there a solution in the usage of BufferedWriter/BufferedOutputStream/FileWriter/FileOutputStream to achieve this?
    Or is there some other way?
    Thank you.

    Ram wrote:
    Do the following with some logic. This is just a blue print to form the logic. Use FileInputStream and FileOutputStream.The blueprint is fine, but I would suggest using a BufferedInputStream and PrintStream(s). That way lines can be read in with readLine() and written out with println(), without the hassle of worrying about newline combinations.
    @OP: Don't worry about efficiency at this stage. Get your program working, and only worry about it then if you think there's a much better solution available. Ram's blueprint only requires you to store one line at a time. You'd be amazed how often the best programs are the simplest ones.
    Winston

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

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

  • 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

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

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

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

  • Delete X number of lines from files

    I am trying to find a way (either through an extension or
    some other third-party freeware) to remove a number of lines from
    the beginning of my files and replace them with alternate code.
    I know that in many cases, a Search and Replace would do the
    trick. But in my case, the lines that I want replaced do not
    exactly match. For example, I might want to remove the first 15
    lines from the beginning of each file in question; however, I might
    have three lines of code in the files that each set a variable
    value, with that value being different in each file. Thus, as far
    as I understand it, the DW Search and Replace would not work for
    me.
    Even if I have can just find some way to first strip the
    lines of code out without replacing it with the new code I want,
    that would be fine. Adding in the new code afterwards would not be
    an issue.
    Any help would be very much appreciated. Thank you very much.
    James

    On Mon, 7 May 2007 17:14:52 +0000 (UTC), "MUSC Webmaster"
    <[email protected]> wrote:
    > Gary: Thanks for the suggestion; however it wasn't quite
    working for me.
    >After your posting, I played around for a bit with
    regular expressions and the
    >best that I could do was get an expression (^.*\r) that
    grabbed all of the
    >first line, with the "r" representing a carriage return
    as opposed to a line
    >feed. If I try to expand on that expression, the search
    results come up with
    >nothing.
    Actually the linfeed is the \n in my suggestion. The ^
    designates the
    beginning of the string.
    Are you on a Mac? I tested it and it worked correctly on my
    windows
    machine. Come to think of it, I have my line feeds set to
    Unix because
    that's what my web server uses. You can try replacing the \n
    with \r or
    \n\r depending on which line feed you're using:
    ^.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r
    or:
    ^.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\ n
    One of the three should work.
    Gary

  • Count the number of lines in a txt file

    I need to count the number of lines in a txt file, but I can't do it using readLine(). This is because the txt file is double spaced. readLine() returns null even if it is not the end of the file. thanks for the help

    I need to count the number of lines in a txt file,
    but I can't do it using readLine(). Then just compare each single byte or char to the newline (code 10).
    This is because the txt file is double spaced. readLine() returns
    null even if it is not the end of the file.Errm what? What do you mean by "double spaced"? Method readLine() should only return null if there's nothing more to read.

Maybe you are looking for

  • SWF error- while generating the flash files.

    Post Author: [email protected] CA Forum: Xcelsius and Live Office Hi, in the old forum I came across the folowing topic: SWF error- while generating the flash files. As I have the same problem, I would like to know the solution / answer / meaning ple

  • Weblogic Server 6.1 with MS Windows 2000 Cluster

    Hello everybody, following situation: We want to implement a Microsoft Cluster (Failover) with MS Cluster Service and 2 nodes (maybe later we try the Weblogic Software clustering with load balancing as a part of an documentum and infotehna DMS). On b

  • Java dis-abled in Safari preferences:  Keep it that way?

    Will I ever need to enable Java in Safari perferences?  Will it be safe if I do?  ( I do have all the current updates.)

  • Required an example excel inputfile for RFBIBL00 LSMW

    I have been reading a lot about LSMW and RFBIBL00, but I could nowhere find an example of the input (excel) file to be loaded into LSMW. I would love to receive example files ([email protected]), adjust the emailaddress for spamfiltering please.

  • Photoshop CS4 Trial Version?

    Hello everyone, for the past hour or so I've been trying to install Photoshop CS4 (trial). Now, I've gone through the installation successfully, but when it was done it said I would need to restart and reinstall to add the missing components. It came