fastest way to create empty file

E

Eric Sosman

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.
 
C

cmk128

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.

just want to create a large file and fill in all zeros
 
C

Christian

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);

?
 
G

Gordon Beaton

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

--
 
E

Eric Sosman

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!!!
 
Z

Zig

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
 
C

cmk128

i found out this can be very fast

RandomAccessFile bo2 = new
RandomAccessFile(PFSSettingConstants.filename, "rw");
bo2.seek(1024*1024*1024);
bo2.write(0);
 
A

Andrew Thompson

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.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
J

Joshua Cranmer

Hi
What is the best way to create a large >10GB empty file using java
API (not native os function call)?
thanks
from Peter ([email protected])

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.
 
C

cmk128

(e-mail address removed) wrote:

..


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.

Hi Andrew Thompson
Sorry about that. I thought creating a empty file, it is no
different in any situation.
thanks
from Peter
 
A

Andrew Thompson

...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.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top