Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Split the file based on the on comparison between line being read from Fil and a constant string.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Split the file based on the on comparison between line being read from Fil and a constant string.

    Hi,

    I have a large file containing records as below :-


    ACCT_NO||ACC_NAME||ADDR||PHONE
    ----------------------------------
    1 abcv adfe 345
    2 erfr ertt 2345
    3 efrtt fkgfdk 23454


    ACCT_NO||ACC_NAME||ADDR||PHONE
    -----------------------------------
    1 abcv adfe 345
    2 erfr ertt 2345
    3 efrtt fkgfdk 23454

    the requirement is to read the file and spilt the large file into chunks when it encounter "ACCT_NO||ACC_NAME||ADDR||PHONE"

    Thanks in adv!

    Regards,
    Hema


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Split the file based on the on comparison between line being read from Fil and a constant string.

    What part of this is giving you trouble? What have you tried? Where exactly are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Split the file based on the on comparison between line being read from Fil and a constant string.

    I have the below code which works fine for just splitting the file according to the size specified.

    package readstring;

    import java.io.BufferedInputStream;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.FileReader;

    import java.io.IOException;



    public class readstring {

    public static void splitFile(File f) throws IOException {

    BufferedInputStream bis = new BufferedInputStream(

    new FileInputStream(f));

    FileOutputStream out;



    String name = f.getName();

    int Counter = 1;

    int sizeOfFiles = 1024 * 1024;// 1MB

    byte[] buffer = new byte[sizeOfFiles];

    int tmp = 0;

    while ((tmp = bis.read(buffer)) > 0) {

    File newFile=new File(f.getParent()+Counter+name);

    newFile.createNewFile();

    out = new FileOutputStream(newFile);

    out.write(buffer,0,tmp);

    out.close();

    }

    }

    public static void main(String[] args) throws IOException {

    splitFile(new File("D:\\My.txt"));

    }

    }

    But i want to split the file whenever it encounters the line
    "ACCT_NO||ACC_NAME||ADDR||PHONE"..

    Not able to compare this string with the input being read from file and split the file based on this..

    I want to know how exactly i can do it..

    Kindly suggest..

    Regards,
    Hema

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Split the file based on the on comparison between line being read from Fil and a constant string.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java program to read last line of a file
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM