|
-
Apr 4th, 2003, 10:58 PM
#1
Thread Starter
Lively Member
Help, need to read a file into a buffer
Im trying to read a file into a buffer. This is exactly what the file holds:
2,3,4,5,6,7
I keep getting a BufferUnderFlowException e. does anyone know where the problem is in the code? My code is below. Sorry, im new to Java! Thanks
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class MyClass
{
public static void main(String[] args)
{
File myFile = new File("D:/numbers.txt");
FileInputStream inFile = null;
try
{
inFile = new FileInputStream(myFile);
}
catch(FileNotFoundException e)
{
System.out.println("FileNotFoundException caught");
System.exit(1);
}
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(22);
try
{
inChannel.read(buf);
buf.flip();
}
catch(IOException e)
{
System.out.println("IOException caught");
System.exit(1);
}
CharBuffer charbuff = buf.asCharBuffer();
char[] array = new char[22];
charbuff.get(array);
System.out.println("yhuyu");
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|