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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Java code Split text file into multiple text file

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

    Default Java code Split text file into multiple text file

    Trying to write Java code to choose text file from computer and split into multiple text file based on field information

    Text file select:

    "First Name","Last Name","Address Line 1","Address Line 2","City","State","ZIP Code","IM Barcode","Sort Position","Tray Number"
    "Stephen","Hight","105 Thurman St","","Carrollton","GA","30117-1955","ADADFTFATTTDADADDFFTAFFFTDDTADDFTFDAFAATDFA AATFATDTFTAAFFAADAFFDT",1,1
    "Kimberly","Kendall","104 Thurman St","","Carrollton","GA","30117-1956","DAAFATDATAFTADDDTTTTADTFATFFDDFTAFDDATDDAAD FTATADADFDTFDAFDDATFDA",2,1
    "James","Boles","108 Thurman St","","Carrollton","GA","30117-1956","DADFDDTFDAFADTFTATADTTDTFATAAFADTATDFTTFAAF TDDFDAFATDTTDTFTFDTTTT",3,1
    "Douglas","Kelley","110 Thurman St","","Carrollton","GA","30117-1956","TFTTTAATDFDAFTDFADFDAFFTTFATATDAAFAATTAFDTD ATFAAFDFAFFTTDTDFFAADD",4,1
    "Christine","Creel","408 Old Mill Dr","","Carrollton","GA","30117-6313","TFDDDTDAFDFFFATDATADFFDAFFTAATTAFDFFTDADDTF TTAFAFATFDATDFTTFTATFF",989,2
    "Elaine","Hobson","410 Old Mill Dr","","Carrollton","GA","30117-6313","DTFFDTAFAFDTADTDDFAFFTDFDTTTDTATDFTDAFTATFA DAAFADDTATTFFTDDTDDFDD",990,2
    "Erica","Tisinger","412 Old Mill Dr","","Carrollton","GA","30117-6313","DFFAAFTDTTADDDATDATATFAFDDFADADFDATTFFAFDDT FFADDTAFTAADTDFDAFADTF",991,2
    "Brandon","Peterson","418 Old Mill Dr","","Carrollton","GA","30117-6313","FTADAAATDATDFTTDTFDFFTTDTTFDTDDDTFAFFATATDF DAADFTDTATDFATAFDADFTT",992,2
    "Robert","Ballard","425 Old Mill Dr","","Carrollton","GA","30117-6314","TFATFFDTADADDAATFTTADFFTFFDFAFAADAAFTAAFDFD FDAFTTFATDFATFFFFTAFAD",993,2
    "Susan","Newbern","429 Old Mill Dr","","Carrollton","GA","30117-6314","AADDFFFFFATADFDTDFFDDDDFTTDTAAFDTADAFDDDAFD ADTFDFDFDDTDTAATTATAAA",994,2
    "M","Motes","431 Old Mill Dr","","Carrollton","GA","30117-6314","TATDAAFTFTTFADFDDFAFATDDAATDADFDFFFTTADDATT AAAFAATAFADDTAFTTFTTFT",995,3
    "Charles","Willis","114 Old Mill Ct","","Carrollton","GA","30117-6317","AFDDTATAADAFFDFFTAATAFTTADDFFDTFFFFDTTADDTT DATDATFFAAAFTFFADFATAD",996,3
    "Nancy","Fox","124 Old Mill Ct","","Carrollton","GA","30117-6317","DFDTATTFFDAAADDDTAATFFFTDFFTFTFFDFDFTFADDDF FADDFDFTAFFADFADTTATFA",997,3
    "Linda","Smith","126 Old Mill Ct","","Carrollton","GA","30117-6317","ATFTDAFTDTTDFTTDAFFDFTTDDADADTDTTFAFFDDATTF TAFAFFDFFADDATTTAFDDTA",998,3
    "Lance","Hatfield","112 Orion Ter","","Carrollton","GA","30117-6318","TDFAFADATAAFFFADATAAFATDTFTTTTAFFFTTFDATDAD ATDAFAFAAFFTAFDFTAFDAA",999,4
    "Dr. Lona","Bibbs","1741 Newnan Crossing Blvd E","","Newnan","GA","30265-1599","TFFTDDTADADTFDFFATFDTTTDAFFTDTDTAFTATFTAADF TFAFADTFAATFTTFTDTTDFT",1000,4


    Split text file:
    split text file1)

    Filename+traynumber 1:

    "First Name","Last Name","Address Line 1","Address Line 2","City","State","ZIP Code","IM Barcode","Sort Position","Tray Number"
    "Stephen","Hight","105 Thurman St","","Carrollton","GA","30117-1955","ADADFTFATTTDADADDFFTAFFFTDDTADDFTFDAFAATDFA AATFATDTFTAAFFAADAFFDT",1,1
    "Kimberly","Kendall","104 Thurman St","","Carrollton","GA","30117-1956","DAAFATDATAFTADDDTTTTADTFATFFDDFTAFDDATDDAAD FTATADADFDTFDAFDDATFDA",2,1
    "James","Boles","108 Thurman St","","Carrollton","GA","30117-1956","DADFDDTFDAFADTFTATADTTDTFATAAFADTATDFTTFAAF TDDFDAFATDTTDTFTFDTTTT",3,1
    "Douglas","Kelley","110 Thurman St","","Carrollton","GA","30117-1956","TFTTTAATDFDAFTDFADFDAFFTTFATATDAAFAATTAFDTD ATFAAFDFAFFTTDTDFFAADD",4,1

    Split Text file 2)
    Filename+traynumber 2:

    "First Name","Last Name","Address Line 1","Address Line 2","City","State","ZIP Code","IM Barcode","Sort Position","Tray Number"
    "Christine","Creel","408 Old Mill Dr","","Carrollton","GA","30117-6313","TFDDDTDAFDFFFATDATADFFDAFFTAATTAFDFFTDADDTF TTAFAFATFDATDFTTFTATFF",989,2
    "Elaine","Hobson","410 Old Mill Dr","","Carrollton","GA","30117-6313","DTFFDTAFAFDTADTDDFAFFTDFDTTTDTATDFTDAFTATFA DAAFADDTATTFFTDDTDDFDD",990,2
    "Erica","Tisinger","412 Old Mill Dr","","Carrollton","GA","30117-6313","DFFAAFTDTTADDDATDATATFAFDDFADADFDATTFFAFDDT FFADDTAFTAADTDFDAFADTF",991,2
    "Brandon","Peterson","418 Old Mill Dr","","Carrollton","GA","30117-6313","FTADAAATDATDFTTDTFDFFTTDTTFDTDDDTFAFFATATDF DAADFTDTATDFATAFDADFTT",992,2
    "Robert","Ballard","425 Old Mill Dr","","Carrollton","GA","30117-6314","TFATFFDTADADDAATFTTADFFTFFDFAFAADAAFTAAFDFD FDAFTTFATDFATFFFFTAFAD",993,2
    "Susan","Newbern","429 Old Mill Dr","","Carrollton","GA","30117-6314","AADDFFFFFATADFDTDFFDDDDFTTDTAAFDTADAFDDDAFD ADTFDFDFDDTDTAATTATAAA",994,2

    Split text file 3)
    Filename+traynumber 3:

    "First Name","Last Name","Address Line 1","Address Line 2","City","State","ZIP Code","IM Barcode","Sort Position","Tray Number"
    "M","Motes","431 Old Mill Dr","","Carrollton","GA","30117-6314","TATDAAFTFTTFADFDDFAFATDDAATDADFDFFFTTADDATT AAAFAATAFADDTAFTTFTTFT",995,3
    "Charles","Willis","114 Old Mill Ct","","Carrollton","GA","30117-6317","AFDDTATAADAFFDFFTAATAFTTADDFFDTFFFFDTTADDTT DATDATFFAAAFTFFADFATAD",996,3
    "Nancy","Fox","124 Old Mill Ct","","Carrollton","GA","30117-6317","DFDTATTFFDAAADDDTAATFFFTDFFTFTFFDFDFTFADDDF FADDFDFTAFFADFADTTATFA",997,3
    "Linda","Smith","126 Old Mill Ct","","Carrollton","GA","30117-6317","ATFTDAFTDTTDFTTDAFFDFTTDDADADTDTTFAFFDDATTF TAFAFFDFFADDATTTAFDDTA",998,3

    Split text file 4)
    Filename+traynumber 4:

    "First Name","Last Name","Address Line 1","Address Line 2","City","State","ZIP Code","IM Barcode","Sort Position","Tray Number"
    "Lance","Hatfield","112 Orion Ter","","Carrollton","GA","30117-6318","TDFAFADATAAFFFADATAAFATDTFTTTTAFFFTTFDATDAD ATDAFAFAAFFTAFDFTAFDAA",999,4
    "Dr. Lona","Bibbs","1741 Newnan Crossing Blvd E","","Newnan","GA","30265-1599","TFFTDDTADADTFDFFATFDTTTDAFFTDTDTAFTATFTAADF TFAFADTFAATFTTFTDTTDFT",1000,4


    Like these, Anyone has an idea how can i write a java code for this??
    Please help me. I am beginners in Java code. My professor told me that simple.
    Please help me guys!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    Where are you having problems?
    Make a list of the steps the code must do to solve the problem.
    Then start with the first item on the list. Code it, compile it, fix errors and execute it. When it works, move to the second item on the list.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    Well, I know first part, To Choose text file from Computer and read that file.
    Confused 2nd part to Create multiple text file samename traynumber1,2.....
    Please help with second part if you can. please..

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

    Default Re: Java code Split text file into multiple text file

    What are the steps the program needs to do? Make a list and start working on the first item in the list.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    Hey, I write the code for first part its work good..
    so its select only text file from computer and read that file.
    But Confused second part what is code for that.. how is look like i don't know..
    please help me..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    But Confused second part what is code for that
    Please explain what is the second step you are having problems with.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    create multiple text files and split text. like in 1 file tray number only 1 and other data and 2 file tray number 2 and other data..... I don't know code for that actually..

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    create multiple text files and split text.
    That sounds like a lot more than a single, simple step. Break the task up into single simple steps.
    Make a list of the steps and work on them one at a time.

    I don't know code for that
    You need the list of steps before you try writing any code.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    Step 1) choose text file from computer = Done
    Step 2) read that text file = Done
    Step 3) look to traynumber where tray number changed create new text files. 1 line is fields it should be same in every text file..== don't know anything about this.. lol

    Please help me.. thanks in advanced!!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    The PrintWriter class is an easy class to use for writing simple text files. Look at its API doc to see how to use it.

    Is step 2) going to read the whole file before the program goes to step 3)?
    What is it going to do with all the lines it reads?
    How is step 3) going to get the lines read by step 2)?
    What will step 3) need to do when it gets a line from the file?

    Will there be any initialization done before any of the steps?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    Well
    Step 2) read whole file but is not required.. I just write code for that.. to understand better..
    soo
    basically need to write code to

    to look over traynumber field.. where tray number changed create new text file.. and split data.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    Which part is a problem now?
    look over traynumber field
    create new text file
    split data.

    That list looks to be in the wrong order and missing some steps.
    How can you get the number without splitting the line into parts?
    You should only create the output file one time at the start and close it one time at the end
    Where is the line written to the file?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    that's part i am trying to tell you..
    1) choose text file.. done
    2) look over that text file and traynumber field.. and how many times tray number changed.. suppose it like 3 times than
    3) make text files...suppose its like 3 time and make 3 text file..
    4) 1st line is fields its should be like header so its should be in every text file
    5) split text data....
    suppose all data have traynumber 1 soo.. all that data goes into textfilename_traynumber1..
    all data have traynumber 2 soo.. all that data goes into textfilename_traynumber2..
    ...............................................

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    2) look over that text file and traynumber field.. and how many times tray number changed.. suppose it like 3 times than
    That will take several steps to do. Break 2) up into the simple steps needed to do it.

    5) split text data....
    That also will take several steps to do. Break 5) up into simple steps.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    If you think that it will take several steps.. so what you want me to do.. do you have any idea.. how can i write code in simple steps..
    please let me know.. really need help actually..

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    You need to break the complex steps up into several simpler steps before trying to write any code.
    How would you do it manually? Given a deck of cards and the task of separating the deck into piles of the 4 suits, how would you explain the steps to some one else to do the task?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    What do you mean?? i didn't understand you!! really sorry for.. its due tomorrow.. I need to finish that today.. its final project.. so please help me..

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    How would you do it manually? Given a deck of cards and the task of separating the deck into piles of the 4 suits, how would you explain the steps to some one else to do the task?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java code Split text file into multiple text file

    dude, I am not telling you to write code for me.. I just trying to tell you that.. which steps are good.. I mean which comes 1, 2, 3.. And you are right, I need to understand step first before write code.. so..

    If you have write this code.. which steps you do..
    how you going to write code for this..I mean 1 thing choose text file.. 2...look for tray number 3...4... 5...

    Please don't get me wrong.. I am not telling you to write code for me.. I just need help.. I am beginners.. soo... please help me..

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    Ok, if you think you know what each of the steps is supposed to do in enough detail,
    start with the next step. You said step 1) was done.
    Write the code for step 2), compile it, fix the errors and then execute it to see if it will execute without errors.
    If you have any problems with getting step 2) to work, paste the code here along with any error messages and any questions you have about it.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    jayraj (April 18th, 2013)

  22. #21
    Junior Member
    Join Date
    Apr 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java code Split text file into multiple text file

    < import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileNameExtensionFilter;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.sql.Connection;
     
    public class choosetextfile11 {
     
    public static void main(String[] args)throws Exception {
     
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter ("Text/Java files", "txt", "java");
    chooser.setFileFilter(filter);
     
    int returnVal = chooser.showOpenDialog(null);
     
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    File f = chooser.getSelectedFile();
    BufferedReader br = new BufferedReader (new FileReader(f));
    String st = "";
    while((st = br.readLine()) != null) {
    System.out.println(st);
    }
     
    }
     
    }
     
    }
    >


    -----

    this is the code tht i wrote.. How can i write the code for just look over the traynumber field..

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    look over the traynumber field..
    The String class has methods that help splitting a line into separate values. Look at the split() method for example.

    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.

  24. #23
    Junior Member
    Join Date
    Apr 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java code Split text file into multiple text file

    so where i need to add that code??

    --- Update ---

    I got confused in this part.. because its has comma in every fields.. so....

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java code Split text file into multiple text file

    look over the traynumber field..
    If the number is in the line that was read from the file, add code to get the number from that line after it was read from the file.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Junior Member
    Join Date
    Apr 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java code Split text file into multiple text file

    didn't get confused again.... if i put comma.. than.. it will read every line.. but we need when traynumber changed.. how many times.. so...

Page 1 of 2 12 LastLast

Similar Threads

  1. How to split a text file in no of chunks ?
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: September 3rd, 2013, 06:51 AM
  2. java code to split text documents into paragraphs and sentences
    By draksha in forum Java Theory & Questions
    Replies: 5
    Last Post: August 17th, 2011, 05:06 AM
  3. probleming writing multiple items to text file
    By gskimmel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2011, 08:41 PM
  4. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 01:40 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM