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.
Andrew Thompson - 09 Dec 2003 11:39 GMT
..
> 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
Josh D.King - 09 Dec 2003 15:07 GMT
> 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
Joseph Dionne - 09 Dec 2003 15:40 GMT
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[].
> ..
>
[quoted text clipped - 15 lines]
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site
Thijs David - 09 Dec 2003 19:24 GMT
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
> 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.
Marco Schmidt - 09 Dec 2003 19:38 GMT
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

Signature
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
Tony Dahlman - 25 Dec 2003 01:50 GMT
> 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:adahlman@jps.net.invalid>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
adahlman - 26 Sep 2007 05:36 GMT
For a much better, and properly indented, version of FileSentenceReader, try:
http://pws.prserv.net/ad/programs/Programs.html*SentenceParser
Tony Dahlman
>> Respected sir,
>>
[quoted text clipped - 93 lines]
>---------------------------------------
>a (no spam)d ahlman( a t )att global( d o t )ne t
adahlman - 26 Sep 2007 05:39 GMT
Sorry. That's:
http://pws.prser.net/ad/programs/Programs.html#SentenceParser
>For a much better, and properly indented, version of FileSentenceReader, try:
>
[quoted text clipped - 7 lines]
>>---------------------------------------
>>a (no spam)d ahlman( a t )att global( d o t )ne t
adahlman - 26 Sep 2007 05:41 GMT
Sorry. That's:
http://pws.prserv.net/ad/programs/Programs.html#SentenceParser
>Sorry. That's:
>http://pws.prser.net/ad/programs/Programs.html#SentenceParser
[quoted text clipped - 4 lines]
>>>---------------------------------------
>>>a (no spam)d ahlman( a t )att global( d o t )ne t
Andrew Thompson - 26 Sep 2007 06:01 GMT
>For a much ...
..easier to follow conversation, refrain from top-posting.
BTW - nice to see your dedication, following that
thread up after nearly four years! I doubt the OP
is still listening, though.

Signature
Andrew Thompson
http://www.athompson.info/andrew/