开发者

how to select input from the drives

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 availab开发者_StackOverflow中文版le.how to call the available drives in the java .no need of buttons thank you.


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

  • Find all drive letters in Java
  • Acquiring drive names (as opposed to drive letters) in Java

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

  • The Swing Tutorial - Components - How to use File Choosers

Related questions

  • Save using JFileChooser with pre-populated file name?
  • JFileChooser.showSaveDialog(…) - how to set suggested file name
  • adjust selected File to FileFilter in a JFileChooser
  • How do I restrict JFileChooser to a directory?
  • Alternative to JFileChooser


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜