java reads native disks and traverses disk files

1. Obtain all local disc information

//1. Get the local character
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
    System.out.print(roots[i].getPath()+"; ");//Disk path
    System.out.print(roots[i].getTotalSpace()/1024/1024/1024+"; ");//Total disk space size
    System.out.print(roots[i].getUsableSpace()/1024/1024/1024+"; ");//Remaining disk space size
    System.out.println(roots[i].getFreeSpace()/1024/1024/1024);//Remaining disk space size
}

The default disk space is BT, and the operating system is 1G=1024MB, 1MB=1024KB, 1KB=1024BT.

The above results are as follows:

C:\; 119; 71; 71
D:\; 299; 233; 233
E:\; 309; 308; 308
F:\; 321; 320; 320
W:\; 588; 358; 358

Among them, the W disk is.

2. Get only local disks (remove network disks, etc.)

File[] roots = File.listRoots();
FileSystemView sys = FileSystemView.getFileSystemView();
for (int i = 0; i < roots.length; i++) {
    if(!sys.getSystemTypeDescription(roots[i]).equals("Local disk")){
        continue;
    }
    System.out.println(roots[i].getPath());// Disk path
}

Output results:

C:\
D:\
E:\
F:\

3. New Files on a Disk

File file = new File("F:\\test\\a\\b");
if (!file.exists()) {
    file.mkdirs();// The directory does not exist. Create the root directory
}

4. Traversing a disk character's file

File file = new File("E:\\");
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
System.out.println(file.getTotalSpace()/1024/1024/1024);// b-kb-M-G
if (file.isAbsolute()) { // Determine whether it is the root directory
    File[] list = file.listFiles(); // Receiving folders with full paths using arrays
    for (int i = 0; i < list.length; i++) {
        System.out.println(list[i].getPath());
        System.out.println(list[i].getTotalSpace()); 
        System.out.println(dateFormat.format(list[i].lastModified()));
        
    }
}

5. Traveling through files in a folder

File file = new File("F:\\test");
getAllFilePath(file);

public static void getAllFilePath(File dir){
    File[] files=dir.listFiles();
    for(int i=0;i<files.length;i++){
        if(files[i].isDirectory()){
            System.out.println(files[i].getPath());
            //The recursive algorithm is used here.  
            getAllFilePath(files[i]);
            
        } else {
            System.out.println(files[i].getPath());
    }
    }
 }

Input results:

F:\test\a
F:\test\a\b

6. Get the size of all files under a disk character or folder

File file = new File("F:\\test");
getAllFileSize(file);

public static long getAllFileSize(File dir){
    File[] files=dir.listFiles();
    for(int i=0;i<files.length;i++){
        if(files[i].isDirectory()){
            //The recursive algorithm is used here.  
            getAllFileSize(files[i]);
        } else {
            sum+=files[i].length();
            System.out.println(files[i]+"Size:"+files[i].length());
    }
    }
    return sum;
 }

7. Save Documents

File filePath = new File("D:\\11.png");
String fileToday = DateUtils.formatTimeYMD(new Date());
//Create directory rootPath
String rootPath = "E:/fileUpload/"+fileToday+"/";
File file = new File(rootPath);
if (!file.exists()) {
    file.mkdirs();//The directory does not exist. Create the root directory
}
String picPath = getPath(rootPath, "videoFile");
saveFile(picPath, filePath);


private static boolean saveFile(String path,File uploadFile) {
    //Files.copy(source, out);//It can be saved directly in this way.
    int bufferSize = 8192;//8kb
    //Writing file
    try {
        File f = new File(path+"/", uploadFile.getName());
        if (!f.exists()) {
            f.createNewFile();
        }
        InputStream in = new BufferedInputStream(new FileInputStream(uploadFile),bufferSize);
        OutputStream out =new BufferedOutputStream(new FileOutputStream(f),bufferSize) ;
        byte[] buffer = new byte[bufferSize];
        int len = 0;
        while ((len = in.read(buffer)) >0) {
            out.write(buffer,0,len);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    
    return true;
}

If you don't need to push progress, you can directly call Files.copy(source, out) method to copy files.

7. Delete files

File filePath = new File("D:/home/logs/backupsystem_error.log");
boolean result = filePath.delete();//true Represents successful deletion
System.out.println(result);

 

Welcome to WeChat public number [Java classics], and watch more Java technology dry goods!

Sweep the map below.

 

Keywords: Java network

Added by rallan on Thu, 03 Oct 2019 10:02:53 +0300