Results 1 to 2 of 2
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Little problem in my program

    Here is a little program that generate garbage files.
    The program first ask you how many files you want. That works.
    Then it ask you the size for the files. If you enter 100, the file(s) will be exactly 100mb large. That works. But it also ask you if you want the file sizes to be random for all the files. That works to, but the generated files are very small, even if I set the maximum size to be 500mb and the minimum size to 499mb. I have generated over 1000 files, and none of them are correct random size, so there is definitely a problem here.

    Java Code:
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class GarbageFile
    {
    	public static void main (String[] args) throws Exception
    	{
    		Scanner in = new Scanner (System.in);
    		String[] theChars = "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z 1 2 3 4 5 6 7 8 9 0 ! \" #  % & / ( ) = ? ^ * _ : ; > < , . - + ".split (" ");
    		String[] theOtherChars = "A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z 1 2 3 4 5 6 7 8 9 0".split (" ");
    		String ls = System.getProperty ("line.separator");
    		short z = 0;
    		double maxsize = 0;
    		double minsize = 0;
    
    		System.out.print ("Please specify the number of file you want: ");
    		double numberOfFiles = controllNumber ();
    
    		if (numberOfFiles == 0)
    			System.exit (0);
    
    		System.out.print ("Please specify the size of the files in megabyte or enter 0 to make each file get a random size: ");
    		double filesize = controllNumber ();
    
    		if (filesize != 0)
    			filesize *= 1048576;
    
    		else
    		{
    			System.out.print ("Please specify the maxiumum random size of for the files in megabyte: ");
    			maxsize = in.nextDouble () * 1048576;
    			in.nextLine ();
    
    			System.out.print ("Please specify the minimum random size of for the files in megabyte: ");
    			minsize = in.nextDouble ();
    			in.nextLine ();
    		}
    
    
    		System.out.print ("Please specify the file extension(including the \".\" character): ");
    		String extension = in.nextLine ();
    
    		/***************************************************************************///
    		if (numberOfFiles == 1)														
    			System.out.println ("Creating file, please wait....");					
    																					
    		else
    			System.out.println ("Creating files, please wait....");
    		/***************************************************************************///
    
    		FileWriter[] f1 = new FileWriter[(int)numberOfFiles];
    		BufferedWriter[] bf1 = new BufferedWriter[f1.length];
    
    		///********************************/// Do this if filesize is not set to random, i.e 0
    		if (filesize != 0)
    		{
    			for (int i = 0; i < bf1.length; i++)
    			{
    				f1[i] = new FileWriter (theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + extension);
    				bf1[i] = new BufferedWriter (f1[i]);
    
    				for (int j = 0; j < filesize; j++)
    				{
    					bf1[i].write (theChars[(int) (84 * Math.random())]);
    					z++;
    
    					if (z == 400)	//New line after 400 characters, so everything wont be printed at one line
    					{
    						bf1[i].write (ls);
    						z = 0;
    					}
    				}
    				bf1[i].flush ();
    			}
    		}
    
    		///********************************/// Do this if filesize is set to 0
    		else
    		{
    			for (int i = 0; i < bf1.length; i++)
    			{
    				f1[i] = new FileWriter (theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + theOtherChars[(int) (62 * Math.random())] + extension);
    				bf1[i] = new BufferedWriter (f1[i]);
    
    				[B][COLOR="#8b0000"]for (int j = 0; j < (int)(maxsize * Math.random()) + minsize; j++)[/COLOR][/B]
    				{
    					bf1[i].write (theChars[(int) (84 * Math.random())]);
    					z++;
    
    					if (z == 400)	//New line after 400 characters, so everything wont be printed at one line
    					{
    						bf1[i].write (ls);
    						z = 0;
    					}
    
    				}
    				bf1[i].flush ();
    			}
    		}
    	}
    
    	public static double controllNumber ()
    	{
    		Scanner in = new Scanner (System.in);
    		double number = 0;
    		while (true)
    		{
    			try
    			{
    				number = in.nextDouble ();
    
    				if (number < 0)//Zero is acceptable, but not negative numbers
    					throw new Exception ();
    
    				break;
    			}
    			catch (Exception e)
    			{
    				System.out.println ("Invaild characters, please re-enter!");
    				in.nextLine ();//Clean the input buffert, or the program will go nuts when wrong number entered
    			}
    		}
    
    		return number;
    	}
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Get the initial number of bytes in the file (before pumping it up to the desired size) by using the length() method of the File class.

    Now subtract the initial number of bytes from the number of bytes desired.

    You have the number of bytes to pump, so now create a byte array with the number of elements being the number of bytes you want to pump.

    Now open a FileWriter to the file and write all of the bytes in your byte array (an enhanced for loop would do).

    File should now be pumped to the correct size.

Similar Threads

  1. Can anyone tell me problem in this program ??
    By sahildave1991 in forum AWT / Swing
    Replies: 3
    Last Post: 07-02-2010, 05:37 PM
  2. what is the problem in this program ?????
    By sahildave1991 in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2010, 01:36 PM
  3. Problem in Program
    By Abbinormal in forum New To Java
    Replies: 9
    Last Post: 01-08-2010, 03:38 AM
  4. Gui program problem..
    By mingming2009 in forum Advanced Java
    Replies: 7
    Last Post: 04-03-2009, 05:15 AM
  5. program problem
    By amith in forum AWT / Swing
    Replies: 12
    Last Post: 05-16-2008, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •