欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java读取本机磁盘及遍历磁盘文件

程序员文章站 2023-04-04 11:39:02
1. 获取本机所有盘符信息 默认获取磁盘空间单位是BT,操作系统是这样算的 1G=1024MB, 1MB=1024KB, 1KB=1024BT 以上运行结果如下: 其中W盘为网盘。 2. 仅获取本地磁盘(除去网络磁盘等) 输出结果: 3.在某一磁盘新建文件 4. 遍历某一个盘符的文件 5.遍历某一文 ......

1. 获取本机所有盘符信息

//1. 获取本机盘符
file[] roots = file.listroots();
for (int i = 0; i < roots.length; i++) {
    system.out.print(roots[i].getpath()+"; ");//磁盘路径
    system.out.print(roots[i].gettotalspace()/1024/1024/1024+"; ");//磁盘总空间大小
    system.out.print(roots[i].getusablespace()/1024/1024/1024+"; ");//剩余磁盘空间大小
    system.out.println(roots[i].getfreespace()/1024/1024/1024);//剩余磁盘空间大小
}

默认获取磁盘空间单位是bt,操作系统是这样算的 1g=1024mb, 1mb=1024kb, 1kb=1024bt

以上运行结果如下:

c:\; 119; 71; 71
d:\; 299; 233; 233
e:\; 309; 308; 308
f:\; 321; 320; 320
w:\; 588; 358; 358

其中w盘为网盘。

2. 仅获取本地磁盘(除去网络磁盘等)

file[] roots = file.listroots();
filesystemview sys = filesystemview.getfilesystemview();
for (int i = 0; i < roots.length; i++) {
    if(!sys.getsystemtypedescription(roots[i]).equals("本地磁盘")){
        continue;
    }
    system.out.println(roots[i].getpath());// 磁盘路径
}

输出结果:

c:\
d:\
e:\
f:\

 3.在某一磁盘新建文件

file file = new file("f:\\test\\a\\b");
if (!file.exists()) {
    file.mkdirs();// 目录不存在,创建根目录
}

4. 遍历某一个盘符的文件

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()) { // 判断是否为根目录
    file[] list = file.listfiles(); // 使用数组接收带有完整路径的文件夹
    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.遍历某一文件夹下的文件

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());
            //这里面用了递归的算法  
            getallfilepath(files[i]);
            
        } else {
            system.out.println(files[i].getpath());
    }
    }
 }

输入结果:

f:\test\a
f:\test\a\b

6. 获取某一个盘符或文件夹下的所有文件的大小

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()){
            //这里面用了递归的算法  
            getallfilesize(files[i]);
        } else {
            sum+=files[i].length();
            system.out.println(files[i]+"的大小:"+files[i].length());
    }
    }
    return sum;
 }

7. 保存文件

file filepath = new file("d:\\11.png");
string filetoday = dateutils.formattimeymd(new date());
//创建目录rootpath
string rootpath = "e:/fileupload/"+filetoday+"/";
file file = new file(rootpath);
if (!file.exists()) {
    file.mkdirs();//目录不存在,创建根目录
}
string picpath = getpath(rootpath, "videofile");
savefile(picpath, filepath);


private static boolean savefile(string path,file uploadfile) {
    //files.copy(source, out);//可以直接用这个方法保存
    int buffersize = 8192;//8kb
    //写文件
    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;
}

如果不需要推送进度的话,可以直接调用files.copy(source, out)方法进行复制文件。

7. 删除文件

file filepath = new file("d:/home/logs/backupsystem_error.log");
boolean result = filepath.delete();//true表示删除成功
system.out.println(result);

 

欢迎关注微信公众号【java典籍】,收看更多java技术干货!

   ▼微信扫一扫下图↓↓↓二维码关注

 java读取本机磁盘及遍历磁盘文件