read information from textfile in string

T

Tina

Hello!

I want to read the content of a .txt-File into a string. The size of
the content is 35 kB so that working with FileReader and
BufferedReader and read the content of the file with readLine() lasts
to long.

Does anybody know, how to do read a .txt-File into a string a little
bit faster?

Thanks in advance, Tina
 
C

Chris Smith

Tina said:
I want to read the content of a .txt-File into a string. The size of
the content is 35 kB so that working with FileReader and
BufferedReader and read the content of the file with readLine() lasts
to long.

Does anybody know, how to do read a .txt-File into a string a little
bit faster?

This is probably fairly close to as fast as you'll get. You could play
around with the buffer size.

public String readTextFile(File f, String encoding)
throws IOException
{
Reader r = new InputStreamReader(
new FileInputStream(f), encoding);
try
{
StringWriter w = new StringWriter();
char[] buffer = new char[65536];

int len;
while ((len = r.read(buffer)) != -1)
{
w.write(buffer, 0, len);
}

return w.toString();
}
finally
{
r.close();
}
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Stefan Ram

Chris Smith said:
StringWriter w = new StringWriter();

I have read (not checked myself) that java.lang.StringWriter
is implemented using java.lang.StringBuffer. So it possibly
will not be faster than java.lang.StringBuffer.

The Java 2 Standard Edition 5 gives us the unsynchronized
java.lang.StringBuilder, which is supposed to be faster than
java.lang.StringBuffer, but some Benchmarks do not always
confirm this. (The toString-operation of StringBuilder is said
to copy characters, which are not copied in StringBuffer.)
 
R

Roedy Green

Does anybody know, how to do read a .txt-File into a string a little
bit faster?

See http://mindprod.com/fileio.html

The usual key for high speed is to do an unbuffered read exactly the
right size reading the whole file is one fell swoop.

If this is really crucial you might see what happens when you
experiment with various buffer sizes, and see if reading raw bytes
then doing the conversion to String yourself helps.
 
T

Thomas Weidenfeller

Tina said:
I want to read the content of a .txt-File into a string. The size of
the content is 35 kB so that working with FileReader and
BufferedReader and read the content of the file with readLine() lasts
to long.

For 35KB? You shouldn't hardly notice a delay.

If you need Java's cross-platform (sort of :) End-Of-Line handling as
provided by readLine(), then this is as fast as you get with what is
build into Java.

If you need the fastest I/O possible, and if you are willing to do your
own EOL handling, give memory-mapped I/O in the java.nio package a try.
Performance of memory-mapped I/O depends, however, very much on your OS
and Java VM. Java's memory-mapped I/O implementation also leaves some
things to be desired, e.g. a way to explicitly unmap a file.

If you don't want to use memory-mapped I/O, consider reading the whole
file into a char array at once. I am in general not a big fan of doing
things like this, because it is to easy to crash an application and a
toy-OS by just providing a very large file as input to such an applications.

/Thomas
 
Y

Yu SONG

Tina said:
Hello!

I want to read the content of a .txt-File into a string. The size of
the content is 35 kB so that working with FileReader and
BufferedReader and read the content of the file with readLine() lasts
to long.

Does anybody know, how to do read a .txt-File into a string a little
bit faster?

Thanks in advance, Tina

You can read the whole content into byte[] and then decode into a string.
You may also try these NIO packages:

java.nio.*
java.nio.channels.*
java.nio.charset.*

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top