File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How can I use a FileChannel and a ByteBuffer to read a UTF string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How can I use a FileChannel and a ByteBuffer to read a UTF string" Watch "How can I use a FileChannel and a ByteBuffer to read a UTF string" New topic
Author

How can I use a FileChannel and a ByteBuffer to read a UTF string

James Marino
Greenhorn

Joined: Jan 19, 2003
Posts: 6
Hi,
Does anyone out there have any idea how I can read UTF strings using java.nio.*?
I working on the developer certification and I want to convert from using pre jdk1.4 to using java.nio. Part of the database file has the following format; a UTF string followed by an int that has the length of the string.
So the pattern of data in the file would be. UTF string, int, UTF string int, �
Now the original project was reading the data something like this.

String[] strings = new String[numStrings];
int lengths = new int[numStrings];
File f = new File(fName);
if (f.exists() && f.canRead() && f.canWrite()) {
db = new RandomAccessFile(f, "rw");

for (int i=0; i <numStrings; i++) {
strings[i] = db.readUTF()
lengths[i] = db.readInt());
}
}
It works fine but I would like to take advantage of java.nio. Here is the best I could come up with.
String[] strings = new String[numStrings];
Int lengths = new int[numStrings];
if (f.exists() && f.canRead() && f.canWrite()) {

db = new RandomAccessFile(f, "rw");
fcDb = db.getChannel();
ByteBuffer buff = ByteBuffer.allocate(maxStringLen);
// wrap ByteBuffer so UTF strings can be read.
ByteArrayInputStream bais = new ByteArrayInputStream(buff.array());
DataInputStream dis = new DataInputStream(bais);


for (int i=0; i <numStrings; i++) {
strings[i] = dis.readUTF()
lengths[i] = dis.readInt());
}
}
It works but is it ever ugly.
Any suggestions on a better way to read a UTF string using buffers and fileChannels?
Many thanks in advance.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Why do you want to change it? It will not get you any extra points, and worse could have you lose points. I would really refrain from changing the Data class like that.
If you would like to get experience with the NIO, then I suggest you use Max's book which has a great explaination of all the NIO stuff, and go through his book and try his examples/project.
But please don't do it in the Data class.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How can I use a FileChannel and a ByteBuffer to read a UTF string