aspose file tools*
The moose likes I/O and Streams and the fly likes RandomAccessFile and a big file read/write problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "RandomAccessFile and a big file read/write problem" Watch "RandomAccessFile and a big file read/write problem" New topic
Author

RandomAccessFile and a big file read/write problem

Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
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

Joined: Dec 02, 2010
Posts: 25
Any idea?

Please help
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
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?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

PatienceIsAVirtue, and please TellTheDetails. How about you start by showing us your copy code?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Andrew Watson
Greenhorn

Joined: Dec 02, 2010
Posts: 25
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Dec 02, 2010
Posts: 25
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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile and a big file read/write problem