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 18 of 18

Thread: csv or text file to Multiple csv or text file

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

    Default csv or text file to Multiple csv or text file

    Have to write Java code to Select CSV File or Text file from Computer and Split into multiple files.. based on field information.. Split file by State

    Please help me!!

    Example:

    robert.txt select this file..

    FirstName,LastName,Address1,Address2,City,State,Zi p
    robert,paul,windihill,,fort myers,FL,30907
    rathin,das,oakbook,apt:2453,fort myers,FL,30907
    Joshua,Arem,160 Crestwood Ct,,Alpharetta,GA,30067
    Soak,Yang,1335 Kilmington Ct,,Alpharetta,GA,30009
    Zerita,Washington,845 Regal Path Ln,Decatur,TN,30087
    Zerita,Washington,845 Regal Path Ln,Decatur,TN,30004
    ......
    .......
    ......
    ....

    Split files..

    1) robert_state_FL

    FirstName,LastName,Address1,Address2,City,State,Zi p
    robert,paul,windihill,,fort myers,FL,30907
    rathin,das,oakbook,apt:2453,fort myers,FL,30907

    2) robert_state_GA

    FirstName,LastName,Address1,Address2,City,State,Zi p
    Joshua,Arem,160 Crestwood Ct,,Alpharetta,GA,30067
    Soak,Yang,1335 Kilmington Ct,,Alpharetta,GA,30009

    3) robert_state_TN

    FirstName,LastName,Address1,Address2,City,State,Zi p
    Zerita,Washington,845 Regal Path Ln,Decatur,TN,30087
    Zerita,Washington,845 Regal Path Ln,Decatur,TN,30004

    ...
    ...
    --------------------
    Trying to do this!!

     
    package com.mkyong.io;
    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;
    import java.sql.DriverManager;
    import java.sql.Statement;
     
    public class split {
     
    	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);
    			}
     
    		}
     
    	}
     
    }


    I know how to choose text file from computer and read that.. but.. I don't know next please help me..


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

    Default Re: csv or text file to Multiple csv or text file

    I don't know next
    Can you explain what the program is supposed to do? Make a list of the steps the program must do to accomplish the task.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: csv or text file to Multiple csv or text file

    Thank you for you replay..

    Program suppose to do..

    1) select text file
    2) read that.. look for State field and count that how many times state changed..
    3) make csv or text files.. that many times as state changed..
    4) name that.. filename_state_statename.txt or csv
    5) first line is same in every text file
    6) import data into files..


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

    Default Re: csv or text file to Multiple csv or text file

    Leave step 1) temporarily (it's a simple call to a class and method to allow the user to select a file) Hard code a file name for testing the rest of the code.
    Step 2) needs further breakdown into simple, single steps. Will there be two passes over the file or will you try to do the task in one pass?

    How does step 6) work? What are the simple steps it must do?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: csv or text file to Multiple csv or text file

    Agree with you man, so you want me do one step at a time..
    I mean i didn't understand second part thing is which command i need to use .. which is better?? to just look over state field and create that many times text file or csv file.. as state changed..

    6) i m sorry import mean split data into files....

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

    Default Re: csv or text file to Multiple csv or text file

    I suggest you write the code one step at a time. Code a step, compile it, fix the errors, execute and fix the errors.
    When the code compiles and executes without any errors, move to the next step.

    Something to think about as the design is made for the program:
    Do you know how many states there are in the file and what those states are?
    If not, the program will need a way to keep track of multiple states and the files where their data is to be written. This would normally be done by using a collection like a Map where the key is the state and the value is an io object for file writing where the line for that state is to be written.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: csv or text file to Multiple csv or text file

    ohh ok thanks i will do that.. do you and link that.. i can read all the commands so i can put that into code.. because i did't know that also.. lol

    which one is better array it good idea to use?? split(,)

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

    Default Re: csv or text file to Multiple csv or text file

    Sorry, I can't see or understand what questions you are asking.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: csv or text file to Multiple csv or text file

    so what you suggest me to use to look over state field??

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

    Default Re: csv or text file to Multiple csv or text file

    Where is the state field located? Is there a way to describe its location so a computer program can consistently get to it? Is it always in the next to the last "column"? columns being delimited by commas.
    If so, then you could use split() and get the state from the next to the last element of the array.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: csv or text file to Multiple csv or text file

    no its not always located next to last column.. but its always in text file or csv file.. so we have to find.. state from file.. because i don't where it located.. soo.. what i can use??

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

    Default Re: csv or text file to Multiple csv or text file

    You need to define how the code can look at a line and find the state in that line. There needs to be rules that control how the data file is created and how a String value for a state is added to that line. Without some rules, it will be very hard to find the value for the state.
    For example:
    Can the state be fully spelled? Or must it be a 2 letter code?
    Must it use one of the 50+ codes defined by the USPS?
    If you don't understand my answer, don't ignore it, ask a question.

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

    robert1994 (April 19th, 2013)

  14. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: csv or text file to Multiple csv or text file

    Its always 2 letters soo...what you want me to use for that?/

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

    Default Re: csv or text file to Multiple csv or text file

    What about the name "Li"? It has 2 letters.
    Having two letters won't be a good enough way to identify the state code. What other criteria can be used to determine if a String is a state code?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: csv or text file to Multiple csv or text file

    Actually we can do with fully state name.. how can i do that?? that's fine..because sometimes its come with fully state names..

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

    Default Re: csv or text file to Multiple csv or text file

    Having more than one way to spell a state will make it much harder. Also are the names always correctly spelled?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: csv or text file to Multiple csv or text file

    its always come with state name like georgia, florida... fully names

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

    Default Re: csv or text file to Multiple csv or text file

    Where can the name be located on the line? Some state names are also names of people: georgia
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. read csv file in java
    By kishore1981 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2012, 07:52 AM
  3. Trying to read a CSV file !
    By Cruz182 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 2nd, 2011, 01:30 PM
  4. Printing name tags from a csv file
    By infoman in forum Java Theory & Questions
    Replies: 8
    Last Post: January 25th, 2011, 05:20 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