简体   繁体   English

使用 Java 获取已安装 USB 插入 Raspberry Pi 4b 的路径

[英]Get path to mounted USB plugged into Raspberry Pi 4b using Java

I am currently writing a program in Java that will be run on a raspberry pi 4b using a docker container.我目前正在 Java 中编写一个程序,该程序将使用 docker 容器在树莓派 4b 上运行。 I am currently trying to figure out how to get the path to the USB so I can move files to and from the USB to the Raspberry Pi.我目前正在尝试弄清楚如何获取 USB 的路径,以便我可以将文件移入和移出 USB 到 Raspberry Pi。 I am using java 17.我正在使用 java 17。

To use an external drive or USB you have to plug the USB into your device first.要使用外部驱动器或 USB,您必须先将 USB 插入您的设备。
You can then run the command lsblk to receive information about all your disks and USB's plugged into your machine.然后,您可以运行命令lsblk以接收有关所有磁盘和 USB 插入计算机的信息。

The output will look something like this: output 看起来像这样:
在此处输入图像描述

To know exactly which is your USB execute the command once before and once after plugging in the USB.要确切知道哪个是您的 USB,请在插入 USB 之前和之后执行一次命令。

You then need to mount the USB to your Linux file system.然后,您需要将 USB 挂载到 Linux 文件系统。 Therefore you create a folder anywhere on your system.因此,您可以在系统的任何位置创建一个文件夹。 But normally you would create the folder in the /mnt or /media folder.但通常您会在/mnt/media文件夹中创建文件夹。

If you did not format your USB beforehand you can follow this tutorial:如果您没有事先格式化您的 USB,您可以按照本教程进行操作:
https://phoenixnap.com/kb/linux-format-disk https://phoenixnap.com/kb/linux-format-disk

Now we return to the output of the lsblk command above.现在我们回到上面lsblk命令的output。 Lets say your USB is named sdb and has a partition named sdb1 .假设您的 USB 名为sdb并且有一个名为sdb1的分区。 Then execute the command mount /dev/sdb1 /media/usb_folder .然后执行命令mount /dev/sdb1 /media/usb_folder

To create your usb folder type mkdir -p /media/usb_folder .要创建 usb 文件夹类型mkdir -p /media/usb_folder You can freely choose the name of the folder but make sure to use the same name throughout the explanation.您可以自由选择文件夹的名称,但请确保在整个说明中使用相同的名称。

You can now use the USB and the path is: /media/usb_folder .您现在可以使用 USB 并且路径是: /media/usb_folder

Sources:资料来源:
https://www.tutorialspoint.com/how-to-mount-usb-drive-in-a-linux-system https://www.tutorialspoint.com/how-to-mount-usb-drive-in-a-linux-system

https://phoenixnap.com/kb/linux-format-disk https://phoenixnap.com/kb/linux-format-disk

https://wiki.ubuntuusers.de/lsblk/ https://wiki.ubuntuusers.de/lsblk/

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

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