Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

file.write...IndexOutOfBoundsException

Posted on 2002-04-25
2
Medium Priority
?
777 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
[X]
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
2 Comments
 
LVL 18

Expert Comment

by:bobbit31
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.

0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 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[]
 
file.write(buffer);

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

file.write(buffer);
 

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

618 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