Tag: 驱动器

使用Java NIO直接访问Windows磁盘

我正在使用一个使用Java NIO的库,以便直接将文件映射到内存,但是我无法直接读取磁盘。 我可以使用带有UNC的FileInputStream直接读取磁盘,例如 File disk = new File(“\\\\.\\PhysicalDrive0\\”); try (FileInputStream fis = new FileInputStream(disk); BufferedInputStream bis = new BufferedInputStream(fis)) { byte[] somebytes = new byte[10]; bis.read(somebytes); } catch (Exception ex) { System.out.println(“Oh bother”); } 但是,我无法将其扩展到NIO: File disk = new File(“\\\\.\\PhysicalDrive0\\”); Path path = disk.toPath(); try (FileChannel fc = FileChannel.open(path, StandardOpenOption.READ)){ System.out.println(“No exceptions! Yay!”); } catch […]

使用java获取驱动器信息时

我尝试了以下程序 import java.io.*; class dr { public static void main(String args[]) { try{ File[] roots = File.listRoots(); for (int index = 0; index < roots.length; index++) { //Print out each drive/partition System.out.println(roots[index].toString()); } } catch(Exception e) { System.out.println("error " +e); } } } 但在我的系统软盘驱动器没有连接,我收到如下消息 “驱动器尚未准备好使用;它的门可能已打开,请检查驱动器A:并确保已插入磁盘并且驱动器门已关闭”然后显示三个选项取消,再次尝试,继续我尝试继续,它的工作原理,但我怎么能避免这个消息

当我尝试在驱动器中搜索时,程序抛出NullPointerException?

我写了一个程序来查找文件或目录。 当我尝试在目录中搜索文件时,它正常工作 例 java FileSearch abc.txt f:\xyz 但是当我尝试从本地驱动器搜索文件而不是程序抛出exception时 java FileSearch abc.txt f:\ 显示所有搜索结果后抛出NullPointerException。 代码是: import java.io.*; class FileSearch{ static String fd; static boolean flg=true; public static void main(String arr[]){ fd=arr[0]; String path=arr[1]; String dir[]=new File(path).list(); new FileSearch().finder(dir,path); if(flg){System.out.print(“File not found.”);} } public void finder(String[] dir,String path){ for(int i=0;i<dir.length;i++){ if(dir[i].equals(fd)){ System.out.println(path+"\\"+fd); flg=false; } if(new File(path,dir[i]).isDirectory()) finder(new […]

如何从java访问磁盘上的特定原始数据

我正在尝试使用以下代码访问原始磁盘中偏移量为50字节的一个字节。 randomAccessFile = new RandomAccessFile(“C:”, “r”); randomAccessFile.seek(50); byte[] buffer = new byte[1]; randomAccessFile.read(buffer); 但我得到的是以下错误: java.io.FileNotFoundException: C: (Acceso denegado) at java.io.RandomAccessFile.open(Native Method) at java.io.RandomAccessFile.(RandomAccessFile.java:212) at java.io.RandomAccessFile.(RandomAccessFile.java:98) at pru.lseek.main(lseek.java:26) 有没有办法从java访问驱动器中的精确字节?