This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

File I/O Streams IndexOutOfBoundsException  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the exception for the following code, though I don't get it why, I tried changing the values for the second argument in the write() method by +-1 but I still got the same Exception. The task is to divide one file into two, and if the length is an odd number the first file should get the bigger half.  Any input would be much appreciated.





 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full exception, including stack trace, and point out which line the exception is thrown from?

Also, can I suggest putting in some debugging lines to print out important values to the console, so you can tell what the value of everything is.
For example:

Otherwise you are just making assumptions about what value everything has.

One thing I will say, available does not tell you how big a file is.
 
Darko Jakimovski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
Thrown at line 24

Also I printed out the values and they are as they should be
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the output from the debugging?
 
Darko Jakimovski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
count at line 13 --> 307
modulo at line 16 --> 1
halfData at line 22 --> 154
 
Darko Jakimovski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's simple, I have a disability I need to address. There is no other explaination to my spending 3 hours on this, cracking my keyboard, smelling like the discharge of a garbage bin and losing half my hair. I'm soooooooooooooooooooooooo mad right now!
I've tried everything that I can possibly think of, red everything there is to read and still I can't understand what's happening and how to solve this in the first place. I also tried dividing the buffer into 2 other byte arrays but that didn't work as well.
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JavaDoc for the FileOutputStream.write call you are using, the last parameter is:
len - the number of bytes to write.

You are asking it to start at byte 154 and write 307 bytes of data, when it only has 153 bytes left.
 
Darko Jakimovski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what it took. The ranch always helps me take a more serious approach to things when I ask a question and you guys ask something back I can't believe what I was doing.  I read what you wrote like maybe 70 times. Thank you for the time spared.
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, again, available() will not work all the time.
The short file you are currently working with is disguising a bug.

available simply tells you how many bytes are available in the inpout stream without blocking, so if there's a delay on reading from the disk then available will not give you the correct total and you will only read part of the file.
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit about if (modulo == 0) ... looks very complicated and error‑prone. Please go through the arithmetic very carefully and work out what indices you are using. Why are you using the extra index when you have an odd‑number‑sized input? What will happen if you use index 155?
Why are you using a file output stream to write a text file? You should use a file writer or a Formatter to write text files.
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, I missed that it was a text file, just concentrated on the immediate issue..
Certain (common) character sets will completely muck up that split by bytes.
 
Darko Jakimovski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Streams because the code is part of an exercise I did about Streams. The whole point of it was to find a way to do what was done using Streams.

About the modulo... Maybe I could have just added it like this without the if statement


I'm using an extra index because the first file that I'm writing to has to get the bigger chunk when bytes are odd. As for using index 155 would be fine because that's half of an even number. I'll just split the byte array in two.

 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!