Format Drive With Java is simple application made to format windows drive.if you are going to make some utility for windows in java or a small project in java then this small application may help you.Source code is free to download.
This project is compiled with netbean 6.9.1. So open it with netbean for quick view.
The trick of format drive in java is exactly the same as in Format drive in C#.
It is totaly based on DOS commands to format a drive. We execute DOS commands to format a drive and provides the necessary parameter like.
- Drive Letter
- Speed (Quick,Normal)
- File System
These parameter provide by this project UI.Below is the special function needs to perform format a drive in java.
First we need the lists of all windows drive so.
Java code to get the list of all windows drive internal or removable.
1 2 3 4 5 6 7 8 |
public void getDname(){ try{ File[] drive =File.listRoots(); for(int i =0;ijComboBox1.insertItemAt(drive[i],i); } } catch(Exception e){} } |
Now all others parameter like format should be quick or normal, file system should be NTFS or FAT32 and what would be the labl of drive after formating.
these all parameter can provided manually through its UI.
For execute format command we will create a batch file which consist of format cammands. Then we will execute this batch file so..
Function to create a batch file consist of format commands
1 2 3 4 5 6 7 8 9 10 |
public void createBatch(String Drive,String speed,String type) { try{ // Create file FileWriter fw = new FileWriter(@“Phoenix.bat”); BufferedWriter bw = new BufferedWriter(fw); bw.write(“Format /y”+” “+Drive+speed+” “+type+” “+ ‘ ‘+jText1.getText()); bw.close();executebatch(); }catch (Exception e){ } } |
Text Version execute batch file contains format commands:
1 2 3 4 5 6 7 8 9 10 11 |
public void executebatch() throws InterruptedException { Process p; try { p = Runtime.getRuntime().exec(@‘Phoenix.bat’); p.waitFor(); File f1 = new File(@‘Phoenix.bat’); f1.delete(); } catch (IOException ex) { } } |
Download source code for free 🙂
[social_lock]https://docs.google.com/file/d/0B5_vqAol5ENfc2Y0eUxrb09HQlU/edit [/social_lock]