123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.ruoyi.common.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.OutputStream;
- /**
- * 文件处理工具类
- *
- * @author ruoyi
- */
- public class FileUtils
- {
- /**
- * 输出指定文件的byte数组
- *
- * @param filename 文件
- * @return
- */
- public static void writeBytes(String filePath, OutputStream os) throws IOException
- {
- FileInputStream fis = null;
- try
- {
- File file = new File(filePath);
- if (!file.exists())
- {
- throw new FileNotFoundException(filePath);
- }
- fis = new FileInputStream(file);
- byte[] b = new byte[1024];
- int length;
- while ((length = fis.read(b)) > 0)
- {
- os.write(b, 0, length);
- }
- }
- catch (IOException e)
- {
- throw e;
- }
- finally
- {
- if (os != null)
- {
- try
- {
- os.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- if (fis != null)
- {
- try
- {
- fis.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- }
- }
- /**
- * @Desc 删除文件
- * @param filePath 文件
- * @return
- */
- public static boolean deleteFile(String filePath)
- {
- boolean flag = false;
- File file = new File(filePath);
- // 路径为文件且不为空则进行删除
- if (file.isFile() && file.exists())
- {
- file.delete();
- flag = true;
- }
- return flag;
- }
- }
|