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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 80
what is a "java.lang.System Property"   ? 20 67
VB Script to add site to Java Exception List 4 78
map related example 6 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 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