Results 1 to 4 of 4

Thread: Help, need to read a file into a buffer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    101

    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");
    }
    }

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    101
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    101
    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
  •  



Click Here to Expand Forum to Full Width