• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile and a big file read/write problem

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I am trying to use RandomAccessFile to read and write big binary file A.dat by parts of 4Mb and write in into file B.dat (by parts too)... But my problem is I get wrong file B.dat length and its data is corrupted.

For example
- file A.dat length is 17.3Mb
- then I write file B.dat with 16Mb length but it should be totally like A.dat (17.3Mb) because I just want to copy A.dat with RandomAccessFile into B.dat

I know I should work around position or seek but I have no idea how to get correct file B length and correct file content.

The problem is I want to copy big files which can be copied by parts so I need to use RandomAccessFile only...

Please give me an example how to use RandomAccessFile in such a case being able to copy file A to file B correctly?

Please help...
 
Andrew Watson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any idea?

Please help
 
Andrew Watson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To present my question more clearly I want to show you an example

I have found this code
http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

but it reads and writes file by 1byte per iteration but 4Mb

How to make this code read each 4Mb from A.dat and write these each 4Mb to file B.dat with no data lost?

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PatienceIsAVirtue, and please TellTheDetails. How about you start by showing us your copy code?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Watson wrote:
I have found this code
http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

but it reads and writes file by 1byte per iteration but 4Mb



It does it a byte at a time with the first example. The article goes on to using the input and output stream methods that use arrays to improve performance, along with a lot of other stuff. What was wrong with the other examples?

Henry
 
Andrew Watson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Andrew Watson wrote:
I have found this code
http://java.sun.com/developer/technicalArticles/Programming/PerfTuning/

but it reads and writes file by 1byte per iteration but 4Mb



It does it a byte at a time with the first example. The article goes on to using the input and output stream methods that use arrays to improve performance, along with a lot of other stuff. What was wrong with the other examples?

Henry



Well I want to make RandomAccessFile read and write file by 4Mb pieces but not 1 byte long
I don't know how to devide the 67Mb file by 4Mb right without loosing the final bytes because that makes file corrupted
I couldn't find any standart example in this area so I hope you can help me.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't see the code I asked for.

Anyway, if you need to write a 67MB file into 4MB chunks, that should require 16 files of 4MB each and one file with the remaining 3MB. I get a feeling that you're forgetting that last file. 17.3MB gives you 4 files of 4MB which merged together gives you the 16MB from your starting post. If you forgot the file with the remaining 1.3MB then you'll miss that 1.3MB when merging.
 
Andrew Watson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:I still don't see the code I asked for.

Anyway, if you need to write a 67MB file into 4MB chunks, that should require 16 files of 4MB each and one file with the remaining 3MB. I get a feeling that you're forgetting that last file. 17.3MB gives you 4 files of 4MB which merged together gives you the 16MB from your starting post. If you forgot the file with the remaining 1.3MB then you'll miss that 1.3MB when merging.



That's right. I get missed the remaining Mbs because it is pretty hard to calculate as for me Especially if all writing is in while loop... The problem is I canot predict the file length so it can be any length... Even smaller than the standart chunk I couldn't find any algorythm which handles the task... So I guess I need some theory advise
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as Rob asked (twice now): can you show us the code that you used previously, the code that didn't work? There's no fundamental reason why this can't be coded; probably you've just got some minor error in the code. But we can't really guess what that is unless you show us what you're doing.
 
reply
    Bookmark Topic Watch Topic
  • New Topic