Reading text files with java.nio.*

H

Hugh Mackay

I am trying to read in lines from a text file which will then be
written to another file with xml tags and transmitted down a modem.
I can move the text file, and get the size but when I read the
contents and print to screen I get meaningless rubbish.
The text file contains lines like:
20040202 165204, 0000.0.001, 00000000h, S Test

but java spits out this:
????????ë?????ë?????????????????ë?????ë?????????????????ë?????ë?????????????????
ë?????ë?????????????????ë?????ë?????????????????ë?????ë?????????

I am using this code:

try
{
alarmFile = new FileInputStream(target);
} catch (FileNotFoundException e) {
System.out.println(e);
}
FileChannel inChannel = alarmFile.getChannel();
try
{
fileSize = (int)inChannel.size();
System.out.println("Filesize = "+fileSize);
} catch (IOException e) {
System.out.println(e);
}
ByteBuffer buf = ByteBuffer.allocate(fileSize);
StringBuffer str = new StringBuffer(fileSize);

if (target.exists())
{
try
{
while(inChannel.read(buf) != -1)
{
str.append(((ByteBuffer)(buf.flip())).asCharBuffer().toString());
buf.clear();
}
alarmFile.close();
}
catch(IOException e)
{
System.out.println(e);
}

Any ideas? AM I missing something blindingly obvious? (you can
probably guess I am not a great java programmer!)
 
C

Chris Smith

Hugh said:
I am trying to read in lines from a text file which will then be
written to another file with xml tags and transmitted down a modem.
I can move the text file, and get the size but when I read the
contents and print to screen I get meaningless rubbish.
The text file contains lines like:
20040202 165204, 0000.0.001, 00000000h, S Test

but java spits out this:
????????ë?????ë?????????????????ë?????ë?????????????????ë?????ë?????????????????
ë?????ë?????????????????ë?????ë?????????????????ë?????ë?????????


Any ideas? AM I missing something blindingly obvious? (you can
probably guess I am not a great java programmer!)

Yep, you're missing something blindingly obvious. A char buffer reads a
series of Java 'char' values, which means two-byte integer values
containing indices into the Unicode table. Unless you happen to have a
text file in UTF-16BE encoding, this won't be what you want.

Instead, you probably want to look into java.nio.charset.CharsetDecoder,
which has a method called decode(ByteBuffer) used to convert a
ByteBuffer into a CharBuffer. That allows you to decode the text
according to the encoding actually used in the source file.

(Alternatively, you could treat System.out as a byte stream and don't do
the text decoding or encoding at all. That would work if you just need
to copy data between streams, but probably won't work if you intend to
parse or otherwise process the data.)

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top