Reading text file String by String

Discussion in 'Java' started by Migrators, Dec 9, 2003.

  1. Migrators

    Migrators Guest

    Respected sir,

    I am having a text File and I have to read that file string by string
    and I have to store these strings I an array of String. How can I
    acheive this.
    Kindly help us.
     
    Migrators, Dec 9, 2003
    #1
    1. Advertising

  2. "Migrators" <> wrote in message
    news:...
    ...
    > I am having a text File and I have to read that file string by string
    > and I have to store these strings I an array of String. How can I
    > acheive this.


    You need to look into the following classes:
    File, InputStream and FileInputStream.

    > Kindly help us.


    Help yourself, start here..
    http://java.sun.com/docs/books/tutorial/essential/io/index.html

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 9, 2003
    #2
    1. Advertising

  3. Migrators

    Josh D.King Guest

    "Andrew Thompson" <> wrote in message news:<sviBb.45949$>...
    > You need to look into the following classes:
    > File, InputStream and FileInputStream.


    If you want to break the Strings down even further, a good class to
    look into would be StringTokenizer
     
    Josh D.King, Dec 9, 2003
    #3
  4. Since I suspect you won't know how many String[] elements are in the
    file, I would check out class ArrayList, then once the file is
    completely read, convert the ArrayList to a String[].

    Andrew Thompson wrote:
    > "Migrators" <> wrote in message
    > news:...
    > ..
    >
    >>I am having a text File and I have to read that file string by string
    >>and I have to store these strings I an array of String. How can I
    >>acheive this.

    >
    >
    > You need to look into the following classes:
    > File, InputStream and FileInputStream.
    >
    >
    >>Kindly help us.

    >
    >
    > Help yourself, start here..
    > http://java.sun.com/docs/books/tutorial/essential/io/index.html
    >
    > --
    > Andrew Thompson
    > * http://www.PhySci.org/ PhySci software suite
    > * http://www.1point1C.org/ 1.1C - Superluminal!
    > * http://www.AThompson.info/andrew/ personal site
    >
    >
     
    Joseph Dionne, Dec 9, 2003
    #4
  5. Migrators

    Thijs David Guest

    You could also try this one :

    File afile = new File ("filepath");
    FileReader fileread = new FileReader(afile);
    BufferedReader bufread = new BufferedReader(fileread)
    String str = new String();
    /*while loop that reads the file line after line untill finished.*/
    while((str = bufread.readLine() != null){
    str... /*do what you want to do with the String*/
    }

    Greetings,
    David

    "Migrators" <> wrote in message
    news:...
    > Respected sir,
    >
    > I am having a text File and I have to read that file string by string
    > and I have to store these strings I an array of String. How can I
    > acheive this.
    > Kindly help us.
    >
     
    Thijs David, Dec 9, 2003
    #5
  6. Migrators:

    >I am having a text File and I have to read that file string by string
    >and I have to store these strings I an array of String. How can I
    >acheive this.


    Wrap a BufferedReader around a FileReader and call BufferedReader's
    readLine method until it returns null. Store the Strings in some sort
    of list (e.g. ArrayList). Then create a string array of the correct
    length and copy the strings into the array.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Dec 9, 2003
    #6
  7. Migrators

    Tony Dahlman Guest

    Migrators wrote:
    >
    > Respected sir,
    >
    > I am having a text File and I have to read that file string by string
    > and I have to store these strings I an array of String. How can I
    > acheive this.
    > Kindly help us.


    Sir, your grammar suggests you are not a native English speaker. So
    I am guessing that by "string" you mean sentence, and that your text
    files may not be in English.

    In that case, no one has yet given you a useful answer. Following is my
    solution of the general case where you want to get all *sentences* from
    a file, whatever the language/locale. The java.text.BreakIterator class
    "should" deal with this problem; however, it is designed to parse String
    objects, not file streams. Hence the following kind of code is needed:
    --------------------------------------------------------------------
    import java.io.*;
    import java.text.*;
    import java.util.*;
    /**
    * Utilities for searching through files.
    * Creation date: (12/22/2003 9:13:07 AM)
    * @author: <A href=mailto:>Tony Dahlman</A>
    */
    public class FileSentenceReader {
    /**
    * Reads through a text file, parsing it into sentences
    * in sound i18n fashion. Returns an ArrayList of the
    * sentences.
    */
    public static ArrayList getAllSentences( File f ) {

    BreakIterator sIter = BreakIterator.getSentenceInstance();
    ArrayList aL = new ArrayList();
    char[] text = new char[1024];

    try{
    BufferedReader ins = new BufferedReader(
    new FileReader( f ) );

    String stub = new String("");
    int chCount = 1024;
    while( chCount > 0 ) {
    chCount = ins.read( text, 0, 1024 - stub.length() );

    // build a string for the iterator from the stream
    StringBuffer sbuf = new StringBuffer( 1024 );
    // reinsert leftover chars from prev loop
    sbuf.append( stub );
    sbuf.append( text );
    String str = sbuf.toString();

    sIter.setText( str );
    int start = sIter.first();
    for( int end = sIter.next();
    end != BreakIterator.DONE;
    start = end, end = sIter.next() ) {
    aL.add( str.substring( start, end ) );
    }
    // remaining chars at end of read buffer
    stub = (String)aL.remove( aL.size() - 1);
    text = new char[1024];
    }
    ins.close();
    } catch( IOException ioe ) {
    ioe.printStackTrace();
    }
    return aL;
    }

    // test code
    public static void main(String[] args) {
    if( args.length != 1 )
    return;

    File f = new File( args[0] );
    if( !f.exists() )
    return;
    if( f.isDirectory() || ! f.canRead() )
    return;

    ArrayList aL = FileSentenceReader.getAllSentences( f );
    System.out.println("Found " + aL.size() + " sentences in file "
    + f );
    Iterator i = aL.iterator();
    while( i.hasNext() )
    System.out.println( (String)i.next() );
    }
    }
    -----------------------------------
    I don't know if this works with right-to-left languages like
    Arabic, so please let me know if it does.

    Good luck and hope this helps. Tony Dahlman
    ---------------------------------------
    a (no spam)d ahlman( a t )att global( d o t )ne t
     
    Tony Dahlman, Dec 25, 2003
    #7
  8. Migrators

    adahlman

    Joined:
    Sep 26, 2007
    Messages:
    1
    FileSentenceReader

    As a brief follow-up, you can find much better code for
    FileSentenceReader at:

    http://pws.prserv.net/ad/programs/Programs.html#SentenceParser

    - Tony DAhlman

     
    adahlman, Sep 26, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    676
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    786
  3. Replies:
    1
    Views:
    453
    Keith Thompson
    Apr 12, 2005
  4. Lionel
    Replies:
    22
    Views:
    655
    Steve Holden
    Feb 3, 2009
  5. Robin Wenger
    Replies:
    191
    Views:
    3,238
Loading...

Share This Page