Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple Java Question

Options
  • 19-10-2004 12:03pm
    #1
    Closed Accounts Posts: 43


    Im writing an object to a disk but I want it to write to the disk until its full then stop.Trying it on a standard floopy disk(dont want to kill the hard disk with any bad code) so the object has to fill the floopy disk so no other file can be placed on it.Im doing it Java but I cannt find how to get it to keep writing til its full then stop.Can anyone point me in the right direction?Ive looked on the web but Im not sure what I should be looking for


Comments

  • Registered Users Posts: 7,580 ✭✭✭uberwolf


    are you looking for it to write over stuff, or is the disk blank when you begin?


  • Closed Accounts Posts: 43 heat


    Itll be overwriting all information on the disk.All that should be left is the object the was writen to the disk with no room for any other information on the disk.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    There is a physical file limit for java. Although offhand I don't recall what it is.

    You could do it easier with batch files.

    filldisk.bat
    @echo off
    echo REM $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ >> filldisk.bat
    echo REM $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ >> filldisk.bat
    echo REM $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ >> filldisk.bat
    filldisk
    

    Then run it and go get a coffee.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    As Hobbes said.

    If you're looking to wipe the disk (i.e. remove all other files),
    Then you'll need to write a program to read all directories, iterate down them, them delete all files that are found.

    I assuming this is not what you want. :D

    If you're looking to fill up a disk with whatever's left, then try writing in blocks.

    So create a new File object.

    Then (forgive me, my Java's a bit rusty)
    while(true) {
         int noKB = 0;
         for(int i = 0; i < 1024; i++) {
               try {  
                  myFile.write(65);
               } catch (IOException e) {
                  System.err.println("Could not write to file: " + e.getMessage());
               }
         }
         try {
                myFile.flush();
         } catch (IOException e) {
                System.err.println("Could not write to file: " + e.getMessage());
                continue;
         }
          noKB++;
          System.out.println(noKB + "KB written to disc\n");
    }
    

    That's just a rough guess. As well as that, this program, when the disk fills, will just continually print "Error writing to file: Out of disk space" or something similar when the disk is full, instead of exiting gracefully.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Actually, this works, writes a 1 MB file to disk.
    Change the test in the while loop to write to the entire disk.
    int noKB = 0;
    		while(noKB < 1024) {
    		     for(int i = 0; i < 1024; i++) {
    		           try {  
    		              myFile.write('a');
    		           } catch (IOException e) {
    		              System.out.println("Could not write to file: " + e.getMessage());
    		           }
    		     }
    		     try {
    		            myFile.flush();
    		     } catch (IOException e) {
    		            System.out.println("Could not write to file: " + e.getMessage());
    		            continue;
    		     }
    		      noKB++;
    		      System.out.println(noKB + "KB written to disc\n");
    		}
    
    Just used eclipse for that. That's a really cool IDE.


  • Advertisement
  • Closed Accounts Posts: 44 Err..


    If you are trying to write the object to disk you would want to use serialization and have your object implement Serializable and overwrite the readObject and writeObject methods.

    If you just want to write data then seamus' way will work. I'd alter it slightly though to write descending sizes of blocks of data to reduce the amount of IO.

    Construct a bunch of byte arrays like this:

    byte[] onemeg = new byte[1024*1024];
    byte[] halfmeg = new byte[512*1024];
    ...
    byte[] onek = new byte[1024];
    ...
    byte onebyte;

    Then try writing the largest array until it fails, then the next largest, and so on until trying to write a single byte fails.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Err.. wrote:
    If you are trying to write the object to disk you would want to use serialization and have your object implement Serializable and overwrite the readObject and writeObject methods.

    If you just want to write data then seamus' way will work. I'd alter it slightly though to write descending sizes of blocks of data to reduce the amount of IO.

    Construct a bunch of byte arrays like this:

    byte[] onemeg = new byte[1024*1024];
    byte[] halfmeg = new byte[512*1024];
    ...
    byte[] onek = new byte[1024];
    ...
    byte onebyte;

    Then try writing the largest array until it fails, then the next largest, and so on until trying to write a single byte fails.
    Abstract. I like it. :)


Advertisement