• JAVA基于File的基本的增删改查


    直接上代码

    public class TestFile {
    
        /**
         *     创建目录
         * @param filename 路径
         */
        public static void createFile(String filename) {
            File file = new File(filename);
            if(!file.exists() && !file.isDirectory()) {
                file.mkdir();
            }
        }
        
        /**
         *     删除目录
         * @param file 文件
         * @throws Exception
         */
        public static void deleteFile(File file) throws Exception {
            if (file.exists()) {//判断文件是否存在 
                if (file.isFile()) {//判断是否是文件 
                    file.delete();//删除文件 
                } else if (file.isDirectory()) {//否则如果它是一个目录 
                    File[] files = file.listFiles();//声明目录下所有的文件 files[]; 
                    for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件 
                        deleteFile(files[i]);//把每个文件用这个方法进行迭代 
                    } 
                    file.delete();//删除文件夹 
                } 
            } else { 
                System.out.println("所删除的文件不存在"); 
            } 
        }
        
        /**
         *     复制文件
         * @param oldPath 旧路径
         * @param newPath 新路径
         * @throws Exception
         */
        public static void copyFile(String oldPath,String newPath) throws Exception{
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            File newFile = new File(newPath);
            if(!newFile.exists()) {
                if (oldfile.exists()) { //文件存在时
                    InputStream inStream = new FileInputStream(oldPath); //读入原文件
                    FileOutputStream fs = new FileOutputStream(newPath);
                    byte[] buffer = new byte[1444];
                    while ( (byteread = inStream.read(buffer)) != -1) {
                        bytesum += byteread; //字节数 文件大小
                        System.out.println(bytesum);
                        fs.write(buffer, 0, byteread);
                    }
                    inStream.close();
                    fs.close();
                }
            }else {
                System.out.println("已存在");
            }
        }
        
        /**
         *    更改文件的名字
         * @param oldpath  旧文件的路径
         * @param newName   新名字
         */
        public static void changeFileName(String oldpath,String newName) {
            File file = new File(oldpath);
            String c = file.getParent();
            File newPath = new File(c + "/" + newName);
            if(newPath.exists()) {
                System.out.println("已存在");
            }else {            
                file.renameTo(newPath);
            }
        }
        
    }

    亲测有效,有误指出,谢谢!

  • 相关阅读:
    Mach-O iOS
    IPA的构建原理 iOS
    输入网址进入网页按回车刷新网页都发生了什么?URL 输入到显示的过程?
    LeetCode另一棵树的子树Swift
    如何解决静态库的冲突问题 iOS
    LeetCode下一个更大元素 I Swift -- 单调递增栈
    UITapGestureRecognizer 和 UICollectionView/UITableView等点击事件冲突问题
    UITableView/UICollectionView调用reloadData刷新时界面闪烁
    C#绘制折线图或曲线图,直接返回Image对象或图片文件
    C#反射+继承+接口的应用
  • 原文地址:https://www.cnblogs.com/bpjj/p/11277197.html
Copyright © 2020-2023  润新知