This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Make a file of desired size using java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Make a file of desired size using java ? " Watch "Make a file of desired size using java ? " New topic
Author

Make a file of desired size using java ?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Please suggest some ways to make java code which generates a file of size entered by the user. The range can be from a few bytes (bits also possible ???) to many megabytes.
Thanks.

SCJP 6. Learning more now.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

What data should the file contain, or does it not matter?

You can just open a FileOutputStream and write as many bytes to it as is necessary. You cannot create a file with a fractional number of bytes (for example, a file with 4 bits).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Jesper de Jong wrote:What data should the file contain, or does it not matter?


Random characters. Does the code change according to the data that this file may contain ? Please give examples.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It might depending on what you want to do. Just open one of many tutorials about I/O and see for yourself.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

You don't need to actually write anything, using RandomAccessFile:
I can use this to create an 8GB file within a second. The file will use 0 to fill the file.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Rob Spoor wrote:You don't need to actually write anything, using RandomAccessFile:
I can use this to create an 8GB file within a second. The file will use 0 to fill the file.


Thanks ! I tried that and it works well.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Thanks ! I needed it for my app here
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Make a file of desired size using java ?
 
Similar Threads
Problem in setBinaryStream when using SequenceInputStream
different between encryption and compression
OCPJP Practice Exam question: Collections
Find string in binary file
Finally Block