FileUtils工具类(byte[] 与File互转 文件夹清空 获取文件名)

FileUtils工具类(byte[] 与File互转 文件夹清空 获取文件名)

package com.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * file工具类 by CHENYB date 2019-08-19
 */
public class File{

    private static final Logger logger = LoggerFactory.getLogger(File.class);


    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
            byte[] b = new byte[8192];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 根据byte数组,生成文件
     */
    public static void getFile(byte[] bfile, String filePath,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+"\\"+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 清空文件夹内所有文件
     * @param path
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 返回后缀名包含.
     */
    public static String getSuffixName(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return originalFilename.substring( originalFilename.lastIndexOf( "." ),originalFilename.length() );
    }

    /**
     * 返回文件名
     */
    public static String getFileName(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return originalFilename.substring( 0, originalFilename.lastIndexOf( "." ));
    }
} 

 Mr.chenyb 随笔记录,方便自己学习

2019-08-19

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,如果不想引用 FileUtils 工具类,也可以使用 Android 提供的 File 和 BufferedReader 类来读取 /proc/stat 文件,然后根据文件中的数据计算 CPU 使用率。下面是一个示例代码: ```java // 获取进程 CPU 使用率 public static float getProcessCpuUsage() { try { // 读取进程的 CPU 时间 int pid = android.os.Process.myPid(); String statFilePath = String.format("/proc/%d/stat", pid); FileInputStream fis = new FileInputStream(statFilePath); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String statContent = br.readLine(); String[] fields = statContent.split(" "); fis.close(); br.close(); // 计算进程的 CPU 使用时间 long utime = Long.parseLong(fields[13]); long stime = Long.parseLong(fields[14]); long cutime = Long.parseLong(fields[15]); long cstime = Long.parseLong(fields[16]); long totalCpuTime = utime + stime + cutime + cstime; // 获取系统总的 CPU 时间 long[] cpuTime = getCpuTime(); long totalSystemCpuTime = cpuTime[0] + cpuTime[1] + cpuTime[2] + cpuTime[3]; // 计算进程的 CPU 使用率 return totalCpuTime * 100f / (totalSystemCpuTime * ProcessorsUtil.getNumberOfCores()); } catch (Exception e) { e.printStackTrace(); } return -1; } // 获取系统 CPU 时间 private static long[] getCpuTime() { try { FileInputStream fis = new FileInputStream("/proc/stat"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String statContent = br.readLine(); String[] fields = statContent.split(" "); long[] cpuTime = new long[fields.length - 1]; for (int i = 0; i < fields.length - 1; i++) { cpuTime[i] = Long.parseLong(fields[i + 1]); } fis.close(); br.close(); return cpuTime; } catch (Exception e) { e.printStackTrace(); } return null; } ``` 需要注意的是,上面的代码只是一个示例,实际使用中还需要根据具体情况进行适当的修改和优化。同时,这种方式获取的 CPU 使用率也可能不够准确,因为它只能反映当前进程占用 CPU 的情况,而且可能会受到其他因素的影响,比如系统负载等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值