zoukankan      html  css  js  c++  java
  • java 实现文件/文件夹复制、删除、移动(一)

    项目快结束了,人清闲了,写写小工具,嘿嘿……

    复制文件

     /**
         * 复制文件到目录 <功能详细描述>
         * 
         * @param srcPath 文件绝对路径
         * @param destDirPath 目标文件夹绝对路径
         * @throws Exception 
         * @see [类、类#方法、类#成员]
         */
        public static void copyFile(String srcPath, String destDirPath) throws Exception
        {
            File srcfile = new File(srcPath);
            File destDir = new File(destDirPath);
    
            InputStream is = null;
            OutputStream os = null;
            int ret = 0;
    
            // 源文件存在
            if (srcfile.exists() && destDir.exists() && destDir.isDirectory())
            {
                try
                {
                    is = new FileInputStream(srcfile);
    
                    String destFile = destDirPath + File.separator + srcfile.getName();
    
                    os = new FileOutputStream(new File(destFile));
    
                    byte[] buffer = new byte[1024];
    
                    while ((ret = is.read(buffer)) != -1)
                    {
                        os.write(buffer, 0, ret); // 此处不能用os.write(buffer),当读取最后的字节小于1024时,会多写;
                        // ret是读取的字节数
                    }
                    os.flush();
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                    throw new Exception("");
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                    throw new Exception("");
                }
                finally
                {
                    try
                    {
                        if (os != null)
                        {
                            os.close();
                        }
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
    
                    try
                    {
                        if (is != null)
                        {
                            is.close();
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            else
            {
                throw new Exception("源文件不存在或目标路径不存在");
            }
        }

    列出文件夹下的所有文件

     /**
         * 列出文件夹下的所有文件,使用递归。 <功能详细描述>
         * 
         * @param dirPath 文件夹绝对路径
         * @see [类、类#方法、类#成员]
         */
        public static void getFileList(String dirPath)
        {
            File rootDir = new File(dirPath);
            if (rootDir.exists())
            {
                File[] files = rootDir.listFiles();
                
                for (File file : files)
                {
                    if (file.isDirectory())
                    {
                        System.out.println("目录" + file.getName());
                        // 递归调用
                        getFileList(file.getPath());
                    }
                    else
                    {
                        System.out.println("文件" + file.getName());
                    }
                }
            }
        }
  • 相关阅读:
    spring 定时任务 taskScheduler详解
    shiro.ini 配置详解
    nginx : server_name localhost 和 chrome : Provisional headers are shown
    参数文件管理
    归档日志管理
    日志文件管理
    控制文件管理
    数据文件管理
    OGG配置参数样例
    OGG配置案例一(源表与目标表结构一致)
  • 原文地址:https://www.cnblogs.com/guoyuqiangf8/p/2779235.html
Copyright © 2011-2022 走看看