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.

Results 1 to 4 of 4

Thread: Create a file specifying the size JAVA

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a file specifying the size JAVA

    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.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Create a file specifying the size JAVA


  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Create a file specifying the size JAVA

    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.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a file specifying the size JAVA

    Quote Originally Posted by JavaPF View Post
    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
    	 */
     
    	//10000000 bytes = 10mb
    	public static int setSize = 10000000;
     
    	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);
     
                    //flush for the file to get updated on size and content
                    output.flush();
     
    		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.
    Last edited by IronWolf; September 20th, 2012 at 10:35 AM.

Similar Threads

  1. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  3. create a file
    By mos33 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 4th, 2009, 02:21 PM
  4. How to create exe file
    By sirimalla in forum Java Theory & Questions
    Replies: 6
    Last Post: November 1st, 2009, 04:07 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM