Operación del archivo de operación de flujo de JAVA IO

No he utilizado la transmisión IO durante mucho tiempo después de trabajar durante tanto tiempo. La aplicación de hoy está un poco oxidada.

1. Java obtiene la lista de nombres de archivo en la ruta del archivo

/**
*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 lee el contenido del archivo

 /**
     * 读取文件内容
     * @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. Crea archivos y directorios

/**
     * (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. Copie archivos del archivo de origen al archivo de destino

/**
     * 拷贝文件从源文件到目的文件
     * @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. Descargue archivos de video y audio de la red a local

/**
     * 网络下载视频&音频文件到本地
     * @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;
        }
    }

Bienvenido a seguir mi cuenta pública de WeChat
Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/CharlesYooSky/article/details/112216013
Recomendado
Clasificación