JAVA IO流操作文件操作

工作这么长时间好久没有使用IO流,今天适用于有点生疏,特此写一写工具类

1、java获取文件路径下的文件名列表

/**
*path 文件路径
*/
 public static List<String> getFilePath(String path) {
    
    
        List<String> pathList = new ArrayList<>();
        File f = new File(path);
        if (!f.exists()) {
    
    
            log.error("文件路径不存在");
            return pathList;
        }
        File fa[] = f.listFiles();
        for (int i = 0; i < fa.length; i++) {
    
    
            File fs = fa[i];
            if (fs.isDirectory()) {
    
    
                pathList.add(fs.getName());
            } else {
    
    
                pathList.add(fs.getName());
            }
        }
        return pathList;
    }

2、java读取文件内容

 /**
     * 读取文件内容
     * @param fileName
     * @return
     */
    public static String readFileContent(String fileName) {
    
    
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
    
    
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
    
    
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
    
    
            log.error("发生异常:{}", e);
            return null;
        } finally {
    
    
            if (reader != null) {
    
    
                try {
    
    
                    reader.close();
                } catch (IOException e1) {
    
    
                    log.error("关闭BufferedReader异常:{}", e1);
                }
            }
            return sbf.toString();
        }
    }

3、创建文件&目录

/**
     * (1)判断文件是否已经存在
     * (2)判断是不是目录
     * (3)判断目录是否存在,不存在则创建
     * (4)创建目标文件
     * @param destFileName
     * @return
     */
    public static boolean createNewFile(String destFileName) {
    
    
        File file = new File(destFileName);
        if (file.exists()) {
    
    
            return false;
        }
        if (destFileName.endsWith(File.separator)) {
    
    
            return false;
        }
        //判断目标文件所在的目录是否存在
        if (!file.getParentFile().exists()) {
    
    
            //如果目标文件所在的目录不存在,则创建父目录
            if (!file.getParentFile().mkdirs()) {
    
    
                return false;
            }
        }
        //创建目标文件
        try {
    
    
            if (file.createNewFile()) {
    
    
                return true;
            } else {
    
    
                return false;
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
            return false;
        }
    }

4、拷贝文件从源文件到目的文件

/**
     * 拷贝文件从源文件到目的文件
     * @param sourcePath
     * @param lastPath
     */
    public static void copyFile(String sourcePath, String lastPath) {
    
    
        try {
    
    
            //1.读取数据输入字节流
            //找到图片路径
            File sourceFile = new File(sourcePath);
            FileInputStream inputStream = new FileInputStream(sourceFile);
            //2.写入数据输出字节流
            File lastFile = new File(lastPath);
            createNewFile(lastPath);
            FileOutputStream outputStream = new FileOutputStream(lastFile);
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
    
    
                outputStream.write(b);
            }
            //关闭流  关闭流原则:先开后关,后开先关。
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

5、网络下载视频&音频文件到本地

/**
     * 网络下载视频&音频文件到本地
     * @param httpUrl
     * @param saveFile
     * @return
     */
    public static boolean httpDownload(String httpUrl, String saveFile) {
    
    
        // 1.下载网络文件
        int byteRead;
        URL url;
        try {
    
    
            url = new URL(httpUrl);
        } catch (MalformedURLException e1) {
    
    
            e1.printStackTrace();
            return false;
        }
        try {
    
    
            //2.获取链接
            URLConnection conn = url.openConnection();
            //3.输入流
            InputStream inStream = conn.getInputStream();
            //3.写入文件
            createNewFile(saveFile);
            FileOutputStream fs = new FileOutputStream(saveFile);

            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
    
    
                fs.write(buffer, 0, byteRead);
            }
            inStream.close();
            fs.close();
            return true;
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
            return false;
        } catch (IOException e) {
    
    
            e.printStackTrace();
            return false;
        }
    }

欢迎关注我的微信公众号
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CharlesYooSky/article/details/112216013