Hi,

I need to write a program which can decrease the free disk space. For eg, if the free disk space is 5GB, the application should ask the user how much space do you want occupy. Application should take the input eg, 2GB and decrease the disk space to 3GB.

Does anyone have any suggestions on how this can be implemented. Can we create a text file of the size specified by the user?

Dani AI

Generated

Quick summary and safety first: wants an app that "occupies" a user-specified amount of free space on a Windows volume. That is doable, but there are a few practical pitfalls missing from the thread so far: file-system limits (FAT32 has a 4 GiB per-file limit), user quotas and permissions, sparse-file semantics (some methods only set a logical file size and do not consume physical space), and the risk of crippling the OS if the system partition is filled. was right to flag cross-platform file-size issues; on Windows you still need to handle quotas and leave a safe free-space margin.

A short checklist of what to do in your Java app:

  • Measure the actual usable bytes for the target directory (use Java’s usable/free-space APIs so quotas and mount restrictions are respected) (java.io.File#getUsableSpace).
  • Validate input so the user cannot request more than usable minus a safety margin (recommend leaving a nontrivial buffer on the system drive).
  • Create the filler in a location where the process has write permission; avoid blindly writing to C:\ root.
  • Delete and close files cleanly to release space; ensure no other process holds handles to them.

Important implementation notes and cautions: simply setting a file length can produce a sparse file on some systems and therefore not actually reduce usable space — see the Windows sparse-file documentation for details (Sparse files). To guarantee real consumption, write actual bytes (in moderate-sized chunks) until the desired allocation is reached. For portability and to avoid filesystem limits like the FAT32 4 GiB per-file cap, break the allocation into multiple files rather than relying on one giant file (this echoes ’s advice). If you want a quick OS-level alternative on Windows for testing, the built-in fsutil can create a file of a given size ().

Final cautions: do not run this on production/system drives unless you know the consequences. Filling a disk can break paging, updates, logging and System Restore; always keep a recovery plan and test on a disposable volume. For background reading on filesystem limits, see FAT32 notes (FAT32).

Recommended Answers

All 4 Replies

Can we create a text file of the size specified by the user?

Yes. Just ask for the size and write that number of bytes to a new file. For large amounts of space it may be safer to create a new directory and fill that with 1GB files, rather than have one file > 2GB

Why would it be safer? I mean what's the risk?

Java is multi-platform, so you can't assume that the OS will support files >2GB

I am only going to use the application for windows. Anyways, I've written the code which works for me:

        BufferedWriter writer = null;
        File file = new File("C:\\DecreaseFreeDiskSpace.txt");
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] roots = File.listRoots();
        RandomAccessFile f = new RandomAccessFile(file, "rw");
        int kb = 1024;
        int mb = 1024*1024;
        int gb = 1024*1024*1024;
        double userInput = 57;
        double setSize = userInput *(gb);
        f.setLength((long) setSize);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.