当前位置: 移动技术网 > IT编程>开发语言>Java > java读取某个文件夹下的所有文件实例代码

java读取某个文件夹下的所有文件实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

java读取某个文件夹下的所有文件

         学习java基础知识,读取文件,读取某个文件夹,需要读取所有文件,如何实现呢,看下实例代码。

实例代码:

import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.file;

public class readfile {
    public readfile() {
    }
    /**
     * 读取某个文件夹下的所有文件
     */
    public static boolean readfile(string filepath) throws filenotfoundexception, ioexception {
        try {

            file file = new file(filepath);
            if (!file.isdirectory()) {
                system.out.println("文件");
                system.out.println("path=" + file.getpath());
                system.out.println("absolutepath=" + file.getabsolutepath());
                system.out.println("name=" + file.getname());

            } else if (file.isdirectory()) {
                system.out.println("文件夹");
                string[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    file readfile = new file(filepath + "\\" + filelist[i]);
                    if (!readfile.isdirectory()) {
                        system.out.println("path=" + readfile.getpath());
                        system.out.println("absolutepath="
                                + readfile.getabsolutepath());
                        system.out.println("name=" + readfile.getname());

                    } else if (readfile.isdirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }

        } catch (filenotfoundexception e) {
            system.out.println("readfile()  exception:" + e.getmessage());
        }
        return true;
    }

    /**
     * 删除某个文件夹下的所有文件夹和文件
     */
    
    
    /*public static boolean deletefile(string delpath)
            throws filenotfoundexception, ioexception {
        try {

            file file = new file(delpath);
            if (!file.isdirectory()) {
                system.out.println("1");
                file.delete();
            } else if (file.isdirectory()) {
                system.out.println("2");
                string[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    file delfile = new file(delpath + "\\" + filelist[i]);
                    if (!delfile.isdirectory()) {
                        system.out.println("path=" + delfile.getpath());
                        system.out.println("absolutepath="
                                + delfile.getabsolutepath());
                        system.out.println("name=" + delfile.getname());
                        delfile.delete();
                        system.out.println("删除文件成功");
                    } else if (delfile.isdirectory()) {
                        deletefile(delpath + "\\" + filelist[i]);
                    }
                }
                file.delete();

            }

        } catch (filenotfoundexception e) {
            system.out.println("deletefile()  exception:" + e.getmessage());
        }
        return true;
    }*/
    
    public static void main(string[] args) {
        try {
            readfile("e:/videos");
            // deletefile("d:/file");
        } catch (filenotfoundexception ex) {
        } catch (ioexception ex) {
        }
        system.out.println("ok");
    }

}


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网