Solved

file.write...IndexOutOfBoundsException

Posted on 2002-04-25
2
755 Views
Last Modified: 2008-03-06
What is causing the exception to be thrown and how do I fix it?

public static void main(String[] args)
{
  try
  {
  FileOutputStream file = new FileOutputStream ("testfile.txt");
  byte[] buffer = "this is string 1".getBytes();  // convert String to byte[]
  int offset = 0;
       
  file.write(buffer,             // buffer
             offset,            // offset
             buffer.length);  // length

   offset += buffer.length;  // add length of what we just wrote to offset

   buffer = "this is string 2".getBytes();  // convert String to byte[]

   System.out.println ("Everything OK so far...");
       
   file.write(buffer,             // buffer
              offset,            // offset
              buffer.length);  // length
   }
   catch (Exception e)
   {
   // throws java.lang.IndexOutOfBoundsException
   System.out.println("Exception: " + e.toString());
   }
}
0
Comment
Question by:jdaues
  • 2
2 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
in your second file write,

offset is the length of the first buffer.

if this + the length of the second buffer is greater than the length of the second buffer, then it'll throw that error.

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Writes len bytes from the specified byte array starting at offset off to this file output stream.

0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
Comment Utility
if you just want to put string1 and string2 together:

byte[] buffer = "this is string 1".getBytes();  // convert String to byte[]
 
file.write(buffer);

buffer = "this is string 2".getBytes();  // convert String to byte[]

file.write(buffer);
 

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now