FranXh
FranXh

Reputation: 4771

java.lang.ArrayOutOfBounds Exception when writing to a file

I have created a method to write in a file, as shown in the code below. This method is called 1000 times from another method, with the two given parameters.

However in 5 of these calls, this method catches an error "e" and prints out: java.lang.ArrayIndexOutOfBoundsException and in my terminal I get the result of System.out.println(""+(double)C/T), but in the file this result is missing. (the other calls work fine)

I was really confused in the beginning, as I am not working with arrays at all. After googling a little bit, I found this: http://www-01.ibm.com/support/docview.wss?uid=swg1IZ87600

I am still really confused and have no idea how to fix this. Somebody help please?

public void myMethod(int C, int T) {   
    try {
      FileOutputStream fout = new FileOutputStream ("output.txt", true );
      PrintStream ps = new PrintStream(fout);    
      System.out.println(""+(double)C/T);
      ps.println((double)C/T+"");
      // close file
      fout.close();
    }
    catch(Exception e) {
        System.err.println(e);
    }   
}  

Upvotes: 0

Views: 920

Answers (2)

duffymo
duffymo

Reputation: 308753

Worked perfectly for me on my Windows JVM:

package cruft;

import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * ArrayIndexOutOfBoundsProblem
 * @author Michael
 * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
 * @since 7/24/12 7:58 PM
 */
public class ArrayIndexOutOfBoundsProblem {

    public static void main(String[] args) {
        ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();

        int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
        int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
        aioob.myMethod(c, t);
    }

    public void myMethod(int C, int T){
        try{
            FileOutputStream fout = new FileOutputStream("output.txt", true );
            PrintStream ps = new PrintStream(fout);
            System.out.println(""+(double)C/T);
            ps.println((double)C/T+"");
            // close file
            fout.close();
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
}

I'd write it this way: conforms to the Java coding standards and uses names that are clearer:

package cruft;

import java.io.*;

/**
 * ArrayIndexOutOfBoundsProblem
 * @author Michael
 * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
 * @since 7/24/12 7:58 PM
 */
public class ArrayIndexOutOfBoundsProblem {

    public static void main(String[] args) {
        ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();

        int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
        int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
        aioob.printValues(c, t);
    }

    public void printValues(int c, int t){
        printValues(System.out, c, t);
    }

    public void printValues(String outputFilePath, int c, int t) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputFilePath);
            PrintStream ps = new PrintStream(fos);
            printValues(ps, c, t);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(fos);
        }
    }

    public void printValues(PrintStream ps, int c, int t) {
        ps.println(String.format("c: %10d t: %10d ratio c/t: %10.4f", c, t, (double) c/t));
    }

    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Upvotes: 1

jonathan.cone
jonathan.cone

Reputation: 6706

Opening and closing a file 1000 times in a tight loop sounds fishy to me, so I would suggest that you start by initializing the FileOutputStream outside the loop, like so:

  private void myMethod() {

    PrintStream fos = null;

    try {
      fos = new PrintStream(new FileOutputStream("output.txt", true));

      for (int i = 0; i < 1000; i++) {
        // Calculate these appropriately...
        int c = 0;
        int t = 0;
        fos.println((double) c / t);
      }

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (fos != null) {
        fos.close();
      }
    }
  }

If this doesn't work, try using a FileWriter instead of a FileOutputStream in the off chance it works around your VM bug.

Upvotes: 1

Related Questions