-
October 21st, 2011, 05:53 PM
#1
[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 . . .
-
October 22nd, 2011, 06:05 AM
#2
Re: RandomAccessFile
If the file is empty the offset needs to be 0.
-
October 23rd, 2011, 01:41 PM
#3
-
October 24th, 2011, 07:26 AM
#4
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.
-
October 24th, 2011, 01:20 PM
#5
Re: RandomAccessFile
Last edited by kolt007; October 24th, 2011 at 01: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|