Good, I wondered if someone could resolve my doubts.
I would like to create a file, specifying the size and the content. That is, 10 Megabytes for example random numbers and content.
A greeting.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Good, I wondered if someone could resolve my doubts.
I would like to create a file, specifying the size and the content. That is, 10 Megabytes for example random numbers and content.
A greeting.
Cross posted
Create a file specifying the size JAVA - Java Forums
db
Hello and welcome to the forums.
Here is a crude example for you. It should give you an idea about what you need to do..
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class FileProg { /** * JavaProgrammingForums.com */ //100000000 bytes = 10mb public static int setSize = 100000000; public static void main(String[] args) throws IOException { FileProg fp = new FileProg(); String newline = System.getProperty("line.separator"); // Create file Writer output = null; File file = new File("file.txt"); output = new BufferedWriter(new FileWriter(file, true)); output.write(""); output.write(newline); // Get file size in bytes long size = fp.getFileSize("file.txt"); // Write file whilst the size is smaller than setSize while(size < setSize){ output.write("Java Programming Forums"); output.write(newline); size = fp.getFileSize("file.txt"); System.out.println(size + " bytes"); } output.close(); System.out.println("Finished at - " + size); } // File size code public long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File does not exist"); return -1; } return file.length(); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
i fear you forgot to flush the string so it will write and size will get updated, but other then that - thank you very much, added the missing flush.
helped alot
--==edit==--
lol, well that extra 0 on the mb number caught me by surprise
careful, thats 100mb and not 10mb
i fixed the digits on my quote of your code.
Last edited by IronWolf; September 20th, 2012 at 10:35 AM.