简体   繁体   English

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

[英]List attached devices on ubuntu in java

I'm a little stumped, currently I am trying to list all of the attached devices on my system in linux through a small java app (similar to gparted) I'm working on, my end goal is to get the path to the device so I can format it in my application and perform other actions such as labels, partitioning, etc. 我有些困惑,目前我正在尝试通过一个正在工作的小型Java应用程序(类似于gparted)列出linux系统上所有已连接的设备,我的最终目标是获取设备的路径因此我可以在应用程序中对其进行格式化,并执行其他操作,例如标签,分区等。

I currently have the following returning the "system root" which on windows will get the appropriate drive (Ex: "C:/ D:/ ...") but on Linux it returns "/" since that is its technical root. 我目前有以下返回“系统根目录”的代码,在Windows上它将获得适当的驱动器(例如:“ C:/ D:/ ...”),但是在Linux上它会返回“ /”,因为这是它的技术根目录。 I was hoping to get the path to the device (Ex: "/dev/sda /dev/sdb ...") in an array. 我希望以数组的形式获取设备的路径(例如:“ / dev / sda / dev / sdb ...”)。

What I'm using now 我现在在用什么

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);
        }
    }
}

Any help or guidance would be much appreciated, I'm relatively new to SO and I hope this is enough information to cover everything. 任何帮助或指导都将不胜感激,我对SO还是比较陌生,我希望这是足够的信息以涵盖所有内容。

Thank you in advance for any help/criticism! 预先感谢您的任何帮助/批评!

EDIT: 编辑:

Using part of Phillip's suggestion I have updated my code to the following, the only problem I am having now is detecting if the selected file is related to the linux install (not safe to perform actions on) or an attached drive (safe to perform actions on) 使用Phillip建议的一部分,我将代码更新为以下内容,我现在遇到的唯一问题是检测所选文件是否与linux安装(对操作不安全)或附加驱动器(对操作安全)有关上)

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));
                }
            }
        }
    }
}

EDIT 2: 编辑2:

Disregard previous edit, I did not realize this did not detect drives that are not already formatted 忽略先前的编辑,我没有意识到这没有检测到尚未格式化的驱动器

Following Elliott Frisch's suggestion to use /proc/partitions I've come up with the following answer. 按照Elliott Frisch的建议使用/ proc / partitions,我提出了以下答案。 (Be warned this also lists bootable/system drives) (请注意,这还会列出可引导/系统驱动器)

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM