Java Forum / General / August 2007
fastest way to create empty file
cmk128@hotmail.com - 11 Aug 2007 12:55 GMT Hi What is the best way to create a large >10GB empty file using java API (not native os function call)? thanks from Peter (cmk128@hotmail.com)
Eric Sosman - 11 Aug 2007 13:50 GMT > Hi > What is the best way to create a large >10GB empty file using java > API (not native os function call)? The "best" way is not to do it at all.
To put it another way, you've asked a tactical question without explaining the strategic context. Why do you want this "empty" (whatever that means) file, and what are you planning to do with it? The circumstances probably influence what "best" means.
 Signature Eric Sosman esosman@ieee-dot-org.invalid
cmk128@hotmail.com - 11 Aug 2007 14:54 GMT > cmk...@hotmail.com wrote: > > Hi [quoted text clipped - 12 lines] > Eric Sosman > esos...@ieee-dot-org.invalid just want to create a large file and fill in all zeros
Christian - 11 Aug 2007 15:16 GMT cmk128@hotmail.com schrieb:
>> cmk...@hotmail.com wrote: >>> Hi [quoted text clipped - 13 lines] > > just want to create a large file and fill in all zeros how about
new RandomAccessFile(file,"rw").getChannel().write(somezeroes, 10L*1024L*1024L*1024L);
?
Eric Sosman - 11 Aug 2007 21:29 GMT >> cmk...@hotmail.com wrote: >>> Hi [quoted text clipped - 9 lines] > > just want to create a large file and fill in all zeros If you're running on a Unix system, just use "/dev/zero" as the file name and you'll have your 10GB of zeroes plus a whole lot of extras!
To put it another way, you *still* haven't explained what you want to do. Your reason for creating this file and your intended use of it are important in choosing a suitable solution. For example, Gordon Beaton has offered a solution elsewhere in this thread, but there's no way for us to know whether it's satisfactory. Explain yourself!!!
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Zig - 11 Aug 2007 22:23 GMT From RandomAccessFile.setLength(long)
If the present length of the file as returned by the length method is smaller than the newLength argument then the file will be extended. In this case, the contents of the extended portion of the file are not defined.
I suspect any solution that attemps to write to the end of a file will work much like setLength() - the contents of the file which you have not explicitly written to will be dependant on OS behavior. The only way to guarantee a zeroed file across all platforms is to zero it out yourself.
Once you have that algorithm in place, you can figure out what OSs you are most likely to be running on, and put guards in place to use functions / assumptions available on that OS that can do something more intelligent.
Eg, on Windows NTFS, all files are normal files unless explicity defined otherwise (IIRC). But, you can always use JNI to create a sparse file, or use Runtime.exec to call "fsutil sparse" to set up a sparse file. Note that JNI is the preferable approach: fsutil does many other things besides sparse file management, and thus has much tighter security restrictions.
HTH,
Zig
cmk128@hotmail.com - 12 Aug 2007 02:22 GMT cmk128@hotmail.com - 12 Aug 2007 02:23 GMT i found out this can be very fast
RandomAccessFile bo2 = new RandomAccessFile(PFSSettingConstants.filename, "rw"); bo2.seek(1024*1024*1024); bo2.write(0);
cmk128@hotmail.com - 12 Aug 2007 02:25 GMT hi Eric Sosman I have written a "file system generater" in java, http://pos.petersoft.com To generate the file system image, i need to create a empty file first, then fill in all the bytes. from Peter
Andrew Thompson - 12 Aug 2007 02:53 GMT ...
> I have written a "file system generater" in java, http://pos.petersoft.com >To generate the file system image, i need to create a empty file >first, then fill in all the bytes. That comment provides loads more information than has so far been known (to anyone besides you) on this thread. I strongly recommend making a habit of including such a statement in the *initial* *post* of threads, as it can ressult in vastly better (and sometimes totally unexpected) technical solutions.
 Signature Andrew Thompson http://www.athompson.info/andrew/
cmk128@hotmail.com - 13 Aug 2007 07:37 GMT > cmk...@hotmail.com wrote: > [quoted text clipped - 15 lines] > > Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1 Hi Andrew Thompson Sorry about that. I thought creating a empty file, it is no different in any situation. thanks from Peter
Andrew Thompson - 13 Aug 2007 08:13 GMT ...
> ...I thought creating a empty file, it is no >different in any situation. It is if you can avoid creating the file at all!
The thing is, often a person thinks 'to get to Z, must use A', where Z is a goal, and A is a strategy.
But sometimes 'Z can be achieved instead by using 'B' (a more advanced form of 'A') or by using both 'X' and 'Y', which completely avoid both 'A' and 'B' and go directly to 'Z' in 1/100th the time!
You seem to be in the mode of 'must use A - how do I do it with A?' That is just plain dumb, and results in long threads where people have to 'tease out' the actual purpose of it all.
I am not saying any particular person always (or even regularly) chooses the wrong strategy, but this being a discussion forum, expect people to be interested, and want to know, what the purpose is. Expect people that want to discuss the entire problem, not just the path that a poster thinks will solve the immediate task.
And if you are about to chime in and argue that this file was invaluable and indispensable and the entire purpose of the program, then ..you've missed my point completely.
 Signature Andrew Thompson http://www.athompson.info/andrew/
Gordon Beaton - 11 Aug 2007 16:57 GMT > What is the best way to create a large >10GB empty file using java > API (not native os function call)? Create a RandomAccessFile, seek to the length you want, write a single byte, then close the file. Any intelligent operating system will create a sparse file with the size you sought to.
/gordon
--
Joshua Cranmer - 12 Aug 2007 16:38 GMT > Hi > What is the best way to create a large >10GB empty file using java > API (not native os function call)? > thanks > from Peter (cmk128@hotmail.com) This is not necessarily possible: not everyone has 10GB of free space, and several filesystems (mostly older ones) do not have the capability to have 10GB files.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Roedy Green - 24 Aug 2007 06:25 GMT >Hi > What is the best way to create a large >10GB empty file using java >API (not native os function call)? Presuming you want to zero it, or fill it with some empty pattern, see http://mindprod.com/jgloss, just do a FileOutputStream to of writes in 100K byte[] chunks. See http://mindprod.com/jgloss/fileio.html for the code.
If you are happy to have junk in the file, use a Random Access file and write just the last byte. See http://mindprod.com/jgloss/fileio.html for the code.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|