Posted on 2002-04-25
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)
  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());
Question by:jdaues
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Expert Comment

ID: 6968775
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.

LVL 18

Accepted Solution

bobbit31 earned 50 total points
ID: 6968788
if you just want to put string1 and string2 together:

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

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



Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JAVA API design with micro service cloud in mind 1 106
java mysql insert application 14 65
Java array 10 85
Bitwise and to sum elements 2 48
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

738 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