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.
Printable View
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..
Code Java: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(); } }
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.