Reading text file String by String

M

Migrators

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.
 
J

Josh D.King

Andrew Thompson said:
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
 
J

Joseph Dionne

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[].
 
T

Thijs David

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
 
M

Marco Schmidt

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
 
T

Tony Dahlman

Migrators said:
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:[email protected]>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() );
}
}
 
Joined
Sep 26, 2007
Messages
1
Reaction score
0
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

Tony Dahlman said:
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:a
(e-mail address removed)lid>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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top