Board Prototype

Attention : Programming, Development, Edtior check content blog in Main. Error be no Running.

Sunday, October 19, 2025

[Java] Create Memory Disk

 mport java.io.FileOutputStream;

import java.io.IOException;

import java.util.Scanner;


public class DiskStorageSimulator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        System.out.print("Enter file name: ");

        String fileName = scanner.nextLine();


        System.out.print("Enter size in MB: ");

        int sizeMB = scanner.nextInt();


        long sizeBytes = sizeMB * 1024L * 1024L;


        try (FileOutputStream fos = new FileOutputStream(fileName)) {

            byte[] buffer = new byte[1024 * 1024]; // 1MB buffer

            for (int i = 0; i < sizeMB; i++) {

                fos.write(buffer);

            }

            System.out.println("File created: " + fileName + " (" + sizeMB + " MB)");

        } catch (IOException e) {

            System.err.println("Error writing file: " + e.getMessage());

        }

    }

}


🧠 What This Does

  • Prompts user for a filename and size in MB
  • Creates a file filled with zero bytes to match the requested size
  • Simulates disk space usage for testing or caching purposes

🛠️ Enhancements You Can Add

  • Progress bar during file creation
  • Random data instead of zeros for testing I/O performance
  • Memory-mapped file using MappedByteBuffer for faster access
  • File existence check to avoid overwriting

📊 Check Disk Space Before Writing

You can also check available disk space using:

long freeSpace = new File("/").getFreeSpace(); System.out.println("Free space: " + (freeSpace / (1024 * 1024)) + " MB");

This ensures you don’t exceed available disk capacity.

Would you like to extend this into a cache system or simulate a RAM disk using Java libraries like Jimfs or Caffeine?

In Java, you can't directly mount a disk like an operating system would with a physical or virtual drive. Mounting is a low-level operation handled by the OS, not by Java itself. However, here are some ways you can simulate or interact with mounted storage from Java:

🧭 What "Mounting a Disk" Means

Mounting refers to making a storage device (like a hard drive, USB, or virtual disk) accessible via the file system. For example:

  • On Windows: a mounted disk appears as D:\ or E:\
  • On Linux: it might be /mnt/disk or /media/usb

Java can access these mounted paths, but it cannot mount or unmount them directly.

🛠️ What You Can Do in Java

✅ Access Mounted Disks

You can read/write files from any mounted location:

File disk = new File("E:\\mydata.txt"); if (disk.exists()) { System.out.println("Disk file found!"); }

✅ Detect Mounted Drives

List all root directories (drives):

File[] roots = File.listRoots(); for (File root : roots) { System.out.println("Drive: " + root.getAbsolutePath()); }

✅ Simulate Mounting with Libraries

Use libraries like Jimfs to simulate a mounted file system in memory.

❌ What Java Cannot Do

  • Mount or unmount physical or virtual disks
  • Create partitions or format drives
  • Interact with OS-level disk management tools (unless using native code via JNI)

🧩 Workaround: Use Native Code

If you need to mount a disk programmatically, you can:

  • Use Java Native Interface (JNI) to call OS-level commands
  • Run shell commands via Runtime.getRuntime().exec() (e.g., mount on Linux)

Would you like help writing a Java wrapper that calls system commands to mount a disk on Linux or Windows?


No comments: