Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中列出ubuntu上连接的设备_Java_Linux_Ubuntu_Java Io - Fatal编程技术网

在java中列出ubuntu上连接的设备

在java中列出ubuntu上连接的设备,java,linux,ubuntu,java-io,Java,Linux,Ubuntu,Java Io,我有点困惑,目前我正试图通过一个小型java应用程序(类似于gparted)在linux中列出我系统上所有连接的设备,我的最终目标是获得设备的路径,以便我可以在应用程序中格式化设备,并执行其他操作,如标签、分区等 目前,我有以下返回“系统根目录”的命令,在windows上它将获得相应的驱动器(例如:“C:/D:/…”),但在Linux上它返回“/”,因为这是它的技术根目录。我希望在数组中获得设备的路径(例如:“/dev/sda/dev/sdb…”) 我现在用的是什么 import java.io

我有点困惑,目前我正试图通过一个小型java应用程序(类似于gparted)在linux中列出我系统上所有连接的设备,我的最终目标是获得设备的路径,以便我可以在应用程序中格式化设备,并执行其他操作,如标签、分区等

目前,我有以下返回“系统根目录”的命令,在windows上它将获得相应的驱动器(例如:“C:/D:/…”),但在Linux上它返回“/”,因为这是它的技术根目录。我希望在数组中获得设备的路径(例如:“/dev/sda/dev/sdb…”)

我现在用的是什么

import java.io.File;

class ListAttachedDevices{
    public static void main(String[] args) {
        File[] paths;

        paths = File.listRoots();

        for(File path:paths) {
            System.out.println(path);
        }
    }
}
任何帮助或指导都将不胜感激,我对SO比较陌生,我希望这些信息足以涵盖所有内容

提前感谢您的帮助/批评

编辑:

根据Phillip的部分建议,我已将代码更新为以下内容,现在唯一的问题是检测所选文件是否与linux安装(不安全执行操作)或连接的驱动器(安全执行操作)相关

导入java.io.File;
导入java.io.IOException;
导入java.nio.file.FileStore;
导入java.nio.file.FileSystems;
导入java.util.ArrayList;
导入javax.swing.filechooser.FileSystemView;
类列表附加设备{
公共静态void main(字符串[]args)引发IOException{
ArrayList dev=新的ArrayList();
对于(文件存储区:FileSystems.getDefault().getFileStores()){
String text=store.toString();
字符串匹配=“(”;
int位置=text.indexOf(匹配);
if(text.substring(position,position+5).equals(“(/dev”)){
if(text.substring(位置,位置+7).equals((/dev/s))){
字符串驱动路径=text.substring(位置+1,text.length()-1);
文件驱动器=新文件(驱动器路径);
dev.add(驱动器);
FileSystemView fsv=FileSystemView.getFileSystemView();
System.out.println(“is(“+drive.getAbsolutePath()+”)根:“+fsv.isFileSystemRoot(drive));
}
}
}
}
}
编辑2:

忽略前面的编辑,我没有意识到这并没有检测到尚未格式化的驱动器

以下建议使用/proc/分区我给出了以下答案。(请注意,这还列出了可引导/系统驱动器)

导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.logging.Level;
导入java.util.logging.Logger;
类列表附加设备{
公共静态void main(字符串[]args)引发IOException{
ArrayList驱动器=新的ArrayList();
BufferedReader br=新的BufferedReader(新文件读取器(“/proc/partitions”);
试一试{
StringBuilder sb=新的StringBuilder();
String line=br.readLine();
while(行!=null){
字符串文本=行;
串驱动路径;
if(text.contains(“sd”)){
int位置=text.indexOf(“sd”);
drivePath=“/dev/”+text.substring(位置);
文件驱动器=新文件(驱动器路径);
驱动器。添加(驱动器);
System.out.println(drive.getAbsolutePath());
}
line=br.readLine();
}
}捕获(IOE异常){
Logger.getLogger(listattachedevices.class.getName()).log(Level.SEVERE,null,e);
}
最后{
br.close();
}
}
}

/dev/sda无法与windows下的c:相媲美。尝试解析“mount”的输出也可以在linux上看到?阅读
/proc/partitions
或运行
blockdev--report
@PhilippLudwig这是一个很好的开始!现在,我们来看看如何检查/dev/s*路径是否是连接的设备(我认为fileIO库对此有一个要求,我现在将对此进行研究)
import java.io.File;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import javax.swing.filechooser.FileSystemView;

class ListAttachedDevices{
    public static void main(String[] args) throws IOException {
         ArrayList<File> dev = new ArrayList<File>();

        for (FileStore store : FileSystems.getDefault().getFileStores()) {

            String text = store.toString();
            String match = "(";
            int position = text.indexOf(match);

            if(text.substring(position, position + 5).equals("(/dev")){
                if(text.substring(position, position + 7).equals("(/dev/s")){
                    String drivePath = text.substring( position + 1, text.length() - 1);
                    File drive = new File(drivePath);
                    dev.add(drive);

                    FileSystemView fsv = FileSystemView.getFileSystemView();

                    System.out.println("is (" + drive.getAbsolutePath() + ") root: " + fsv.isFileSystemRoot(drive));
                }
            }
        }
    }
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

class ListAttachedDevices{
    public static void main(String[] args) throws IOException {
        ArrayList<File> drives = new ArrayList<File>();
        BufferedReader br = new BufferedReader(new FileReader("/proc/partitions"));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                String text = line;
                String drivePath;

                if(text.contains("sd")){
                    int position = text.indexOf("sd");
                    drivePath = "/dev/" + text.substring(position);
                    File drive = new File(drivePath);
                    drives.add(drive);
                    System.out.println(drive.getAbsolutePath());
                }

                line = br.readLine();
            }
        } catch(IOException e){
            Logger.getLogger(ListAttachedDevices.class.getName()).log(Level.SEVERE, null, e);
        }
        finally {
            br.close();
        }
    }
}