1. 区分本地磁盘,可移动磁盘,驱动盘
代码如下 | 复制代码 |
package com.lvjava; import java.io.File; import javax.swing.filechooser.FileSystemView; public class FileSystemTest { private final static String localDiskName = "本地磁盘"; private final static String removableDiskName = "可移动磁盘"; private final static String enLocalDiskName = "Local Disk"; private final static String enRemovableDiskName = "Removable Disk"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FileSystemView fileSystemView = FileSystemView.getFileSystemView(); File[] roots = File.listRoots(); for (File file : roots) { String diskName = fileSystemView.getSystemTypeDescription(file); System.out.println(diskName); if (diskName.startsWith(localDiskName) || diskName.startsWith(enLocalDiskName)) {// 当磁盘为本地磁盘或可移动磁盘 //Do something } else if (diskName.startsWith(removableDiskName) || diskName.startsWith(enRemovableDiskName)) { //Do something } } } } |
运行代码输出如下:
本地磁盘
本地磁盘
本地磁盘
本地磁盘
CD 驱动器
CD 驱动器
可移动磁盘
2. Java代码复制文件
代码如下 | 复制代码 |
/** * Moving a File to Another Directory * @param srcFile eg: c:windowsabc.txt * @param destPath eg: c:temp * @return success */ public static boolean move(String srcFile, String destPath){ // File (or directory) to be moved File file = new File(srcFile); // Destination directory File dir = new File(destPath); // Move file to new directory and return result return file.renameTo(new File(dir, file.getName())); } |
下面分享一个类,可以复制文件夹与文件
代码如下 | 复制代码 |
package com.xuanwu.mtoserver.util; import java.io.*; /** public static void main(String args[]) throws IOException { if (type.equalsIgnoreCase("txt"))// 转码处理 // 复制文件 // 新建文件输出流并对它进行缓冲 // 缓冲数组 // 复制文件夹 /** /** |