una quemar portatil para musica lector laptop grabar externo disco como archivos java

java - quemar - lector de cd externo para portatil



¿Cómo puedo obtener una lista de todas las unidades pero también obtener el tipo de unidad correspondiente(extraíble, disco local o cd-rom, dvd-rom... etc)? (7)

Con este código podrás obtener todas las unidades y su descripción tipo.

File[] paths; FileSystemView fsv = FileSystemView.getFileSystemView(); // returns pathnames for files and directory paths = File.listRoots(); // for each pathname in pathname array for(File path:paths) { // prints file and directory paths System.out.println("Drive Name: "+path); System.out.println("Description: "+fsv.getSystemTypeDescription(path)); }

¿Cómo puedo obtener una lista de todas las unidades pero también el tipo de unidad correspondiente (extraíble, disco local o cd-rom, dvd-rom ... etc)?


Este fragmento de código funciona en Windows. Informa correctamente que las unidades de lápiz USB son extraíbles, pero en mi computadora portátil, un disco duro USB está marcado como no extraíble. De todos modos, este es el mejor que he encontrado sin usar código nativo.

for (Path root : FileSystems.getDefault().getRootDirectories()) { FileStore fileStore = Files.getFileStore(root); System.out.format("%s/t%s/n", root, fileStore.getAttribute("volume:isRemovable")); }

Fuente: este archivo que aparentemente proviene del JDK.


La forma correcta es cómo respondió Luciano con la propiedad isRemovable, pero recientemente gané 10 que si comenzaba desde USB, entonces los USB ya no se encontraban en ese método.

Por lo tanto, utilicé, solo para Windows, las siguientes llamadas wmic:

ArrayList<File> rootDirs = new ArrayList<File>(); if( isWin ) { if( i_dolog ) LOG.info( "Windows found as OS." ); ArrayList<String> partids = new ArrayList<String>(); String results0 = execute( "wmic diskdrive where interfacetype=''USB'' assoc /assocclass:Win32_DiskDriveToDiskPartition" ); String lines[] = results0.split( "/r/r/n" ); for( String line : lines ) { String test = "Win32_DiskPartition.DeviceID="; int index = line.indexOf( test ); if( index >= 0 ) { String partid = line.substring( index + test.length() + 1 ); partid = partid.substring( 0, partid.indexOf( ''"'' ) ); partids.add( partid ); } } for( String partition : partids ) { String results2 = execute( "wmic partition where (DeviceID=''" + partition + "'') assoc /assocclass:Win32_LogicalDiskToPartition" ); String lines2[] = results2.split( "/r/r/n" ); for( String line : lines2 ) { String test = "Win32_LogicalDisk.DeviceID="; int index = line.indexOf( test ); if( index >= 0 ) { String partid = line.substring( index + test.length() + 1 ); partid = partid.substring( 0, partid.indexOf( ''"'' ) ); rootDirs.add( new File( partid + "//" ) ); } } } }


No hay definición de lo que estás preguntando.

Tengo una memoria USB que, cuando la conecto, aparece como un CD-ROM. Después de ejecutar un programa en el CD-ROM, se adjunta una segunda partición que parece ser un disco duro.

Tengo unidades de CD-ROM extraíbles. También tengo unidades de disco duro eSATA "internas" en el exterior de mi computadora.

Tendría que trabajar bastante duro para obtener una definición de enlace para el "tipo" de la unidad. ¿Por qué no nos dice qué está tratando de hacer, en lugar de preguntar sobre la forma particular en que desea hacerlo?


Si usa JACOB , puede enumerar todas las unidades junto con los tipos apropiados:

import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.EnumVariant; import com.jacob.com.JacobObject; import com.jacob.com.Variant; import java.io.File; import java.util.ArrayList; import java.util.List; public class DrivesExample { public interface HasNativeValue { int getNativeValue(); } public enum DriveTypeEnum implements HasNativeValue { Unknown(0), NoRootDirectory(1), RemovableDisk(2), LocalDisk(3), NetworkDrive(4), CompactDisc(5), RAMDisk(6); public final int nativeValue; DriveTypeEnum(int nativeValue) { this.nativeValue = nativeValue; } public int getNativeValue() { return nativeValue; } } private static <T extends Enum<T> & HasNativeValue> T fromNative(Class<T> clazz, int value) { for (T c : clazz.getEnumConstants()) { if (c.getNativeValue() == value) { return c; } } return null; } /** * The drive information. */ public static final class Drive { /** * File system on the logical disk. Example: NTFS. null if not known. */ public final String fileSystem; /** * Value that corresponds to the type of disk drive this logical disk represents. */ public final DriveTypeEnum driveType; /** * The Java file, e.g. "C:/". Never null. */ public final File file; public Drive(String fileSystem, DriveTypeEnum driveType, File file) { this.fileSystem = fileSystem; this.driveType = driveType; this.file = file; } @Override public String toString() { return "Drive{" + file + ": " + driveType + ", fileSystem=" + fileSystem + "}"; } } /** * Lists all available Windows drives without actually touching them. This call should not block on cd-roms, floppies, network drives etc. * * @return a list of drives, never null, may be empty. */ public static List<Drive> getDrives() { List<Drive> result = new ArrayList<>(); ActiveXComponent axWMI = new ActiveXComponent("winmgmts://"); try { Variant devices = axWMI.invoke("ExecQuery", new Variant("Select DeviceID,DriveType,FileSystem from Win32_LogicalDisk")); EnumVariant deviceList = new EnumVariant(devices.toDispatch()); while (deviceList.hasMoreElements()) { Dispatch item = deviceList.nextElement().toDispatch(); String drive = Dispatch.call(item, "DeviceID").toString().toUpperCase(); File file = new File(drive + "/"); DriveTypeEnum driveType = fromNative(DriveTypeEnum.class, Dispatch.call(item, "DriveType").getInt()); String fileSystem = Dispatch.call(item, "FileSystem").toString(); result.add(new Drive(fileSystem, driveType, file)); } return result; } finally { closeQuietly(axWMI); } } private static void closeQuietly(JacobObject jacobObject) { try { jacobObject.safeRelease(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] arguments) { List<Drive> drives = getDrives(); for (Drive drive : drives) { System.out.println(drive.toString()); } } }

Ejemplo de salida:

Drive{C:/: LocalDisk, fileSystem=NTFS} Drive{D:/: LocalDisk, fileSystem=NTFS} Drive{E:/: RemovableDisk, fileSystem=NTFS} Drive{F:/: RemovableDisk, fileSystem=FAT32} Drive{G:/: RemovableDisk, fileSystem=null} Drive{Y:/: NetworkDrive, fileSystem=NTFS}

Para usar JACOB , agregue los archivos JAR y DLL de la descarga como bibliotecas en su proyecto. Esta solución es solo para Windows por supuesto.


Suponiendo que sean ventanas, use File.listRoots() para obtener todas las raíces. Luego use FileSystemView para verificar si es un disquete o una unidad. Aparte de eso no tengo ni idea.


File[] roots = File.listRoots(); for(int i=0;i<roots.length;i++) System.out.println("Root["+i+"]:" + roots[i]);