|
-
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");
}
}
-
Apr 5th, 2003, 02:35 PM
#2
Dazed Member
Put you code in the [code] [fowardslash-code] blocks. It will help other people to read it. Are you trying to buffer the streams or are you trying to read the contents of the file into a byte[] array? It's sounds like the latter but i just want to be sure. To buffer the stream use either a BufferInputStream or a BufferedReader.
The problem sounds like you are not allocating enough memory. What line is the error being produced? The compiler should tell you.
-
Apr 5th, 2003, 03:39 PM
#3
Thread Starter
Lively Member
this is the erroe message that i am getting :
exception in thread "main" java.nio.BufferUnderflowExcepitonError at java.nio.CharBuffer.get(CharBuffer:java:609)
java.nio.CharBuffer.get(CharBuffer:java:633)
at MyClass.main(MyClass.java:34)
that is it! thanks
-
Apr 5th, 2003, 03:42 PM
#4
Thread Starter
Lively Member
what i am trying to do is read the one line in a text file and put into a buffer. and then extract the contents of the file that is in the buffer into a string so that i can parse it and then do some arithmetic on the numbers. i am in desperate need of help. thanks
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
|