2

i have to write the code using simple java.i have to copy one source file to the destination file.the problem is that the user have to select the source and destination from any drives available.how to call the available drives in the java .no need of buttons thank you.

adithi
  • 145
  • 5
  • 12

2 Answers2

5

java.io.File has a static utility method listRoots() that returns a File[] of available filesystem roots.

From the documentation:

A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely "/". The set of available filesystem roots is affected by various system-level operations such as the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.

Here's an example snippet:

    import java.io.*;

    //...
    for (File root : File.listRoots()) {
        System.out.println(root.getAbsolutePath());
        System.out.println(root.getTotalSpace());
    }

This prints the absolute path and total space of each of the system's filesystem root.

Related questions


Swing GUI component

If you are using Swing for GUI, there's javax.swing.JFileChooser which you may be able to use. It's highly customizable (e.g. file extension filtering), and you can use it to select files and/or directories for saving and/or loading.

See also

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
4

Use the File.listRoots() method to list all the drives.

naikus
  • 23,523
  • 4
  • 39
  • 43