[RESOLVED] RandomAccessFile
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] RandomAccessFile

  1. #1
    Join Date
    Sep 2011
    Posts
    197

    [RESOLVED] RandomAccessFile

    With this.

    Code:
    public static void main(String[] args) throws IOException {
      
      String s = "hello world";
      byte[] sT = {'h','i'};
      Path path = FileSystems.getDefault().getPath
    
    ("D:/Users/Kolton/Desktop/codebreaker/learn/Learnpath/Test2.txt"); 
    
      try 
       (RandomAccessFile rA = new RandomAccessFile(file, "rw")) {
      rA.write(sT, 1, 2);
           } 
          }
         }
    I get this error

    Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException
            at java.io.RandomAccessFile.writeBytes(Native Method)
            at java.io.RandomAccessFile.write(RandomAccessFile.java:499)
            at Test.main(Test.java:103)
    Press any key to continue . . .

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: RandomAccessFile

    If the file is empty the offset needs to be 0.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2011
    Posts
    197

    Re: RandomAccessFile

    The file isn't empty

  4. #4
    Join Date
    Oct 2011
    Posts
    12

    Re: RandomAccessFile

    Such an error (IndexOutOfBounds) usually means that at some point in your code, typically in a loop, you exceed the maximum number of elements in an array or another constant size data structure. In your case, I guess the error occurs because you are trying to copy 2 characters from an array that contains only 2 elements, and you start from the second element in that array, which means you are trying to copy a third element, which doesn't exist.

    Hope that helps.

  5. #5
    Join Date
    Sep 2011
    Posts
    197

    Re: RandomAccessFile

    Edit
    Last edited by kolt007; October 24th, 2011 at 02:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center