Results 1 to 7 of 7
  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default How to read text after delimiter in textfile ?

    HI
    I HAVE TEXT FILE, there will be only two columns
    e.g: test, PS_JOB

    all i have to do is read text after delimiter i have done following

    Java Code:
     Scanner scan = new Scanner(new File("textfile.txt"));
                             	
                            scan.useDelimiter(",");       
                            while(scan.hasNext()){
                                      
                        System.out.println(scan.next());  
                       
                              }
    this returns every value. i want only after delimiter.

    Regards
    Sandeep

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Why not read entire line and use String.split?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I just realised your misconception. Using the delimeter does not automagically ignore text before and give you the text afterwards. What it does, it uses the delimeter as the break point instead of a line break.

    If the text was "one,two,three". Using a comma as a delimeter you would get three values "one" "two" and "three". Without the delimeter you would get one value "one,two,three".

  4. #4
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hi junky,

    how to print that split one, two , three is my doubt ?

    Regrds
    Sandeep

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Split on the comma, i.e. line.split(","); the split method returns an array of Strings where array[0] == "one", array[1] == "two" etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    return is offline Member
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    how big is the file? If not too big, I would read each line into a string, and then invoke the substring method using the index of the "," + 1 as the start index. You can use indexOf to get the index of the comma and then add 1.

  7. #7
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hi JosAh,

    i got it i used split and pushed it, thanks a lot guys

    and return, yea the file is huge say arnd 38000 to 100000 lines to read.

    Sandeep

Similar Threads

  1. read text from .pdf
    By seghri in forum Java Applets
    Replies: 0
    Last Post: 05-08-2011, 03:05 PM
  2. how to read only parts of textfile by java
    By smn in forum SWT / JFace
    Replies: 2
    Last Post: 10-08-2010, 03:31 AM
  3. Read text file
    By Sean04 in forum New To Java
    Replies: 18
    Last Post: 06-16-2010, 01:43 AM
  4. How to read DatePicker Text box value in jsf?
    By kishan in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 05-12-2010, 04:23 PM
  5. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •