栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 对文件进行相关操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 对文件进行相关操作

Java 对文件进行相关操作 整理比较常见的方法

方法一:读取指定文件中的内容

	
    public static String readContent(String path) {
        File file = new File(path);

        // result 用来存放读取到的内容
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;

            // readLine方法表示一行一行的读取内容
            while ((s = br.readLine()) != null) {

                // 将内容放入result中
                result.append(System.lineSeparator() + s);
            }

            // 关闭资源
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 去掉内容中的换行字符
        return result.toString().replaceAll("r|n", "");
    }

方法调用:

readContent("D:\MyComputer\小辰哥哥.txt")

方法二:写入文件中,并覆盖其原内容

	
    public static boolean writeFile(String Content, String filepath) {
        boolean flag = false;
        try {

            // 写入的文件的路径
            PrintWriter pw = new PrintWriter(filepath);

            // 写入的内容
            pw.write(Content);
            pw.flush();
            pw.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

方法调用:

writeFile("Hello World", "D:\MyComputer\小辰哥哥.txt")

方法三:删除某个文件夹下的所有文件夹和文件

	
    public static boolean deletefile(String delpath) {
        try {
            File file = new File(delpath);
            if (!file.isDirectory()) {
                file.delete();
                System.out.println("单文件已删除");
            } else if (file.isDirectory()) {
                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("删除文件绝对路径:" + delfile.getAbsolutePath());
                        System.out.println("删除文件名称:" + delfile.getName());
                        delfile.delete();
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + "\" + filelist[i]);
                    }
                }
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

方法调用:

deletefile("D:\MyComputer\删除测试")

方法四:读取某个文件夹下的所有文件

	
    public static boolean readfile(String filepath) {
        try {
            File file = new File(filepath);
            if (file.isDirectory()) {
                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("文件名称:" + readfile.getName());
                        System.out.println("文件绝对路径:" + readfile.getAbsolutePath());
                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\" + filelist[i]);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

方法调用:

readfile("D:\MyComputer\读取文件")

总结

每天一个提升小技巧!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317889.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号