java 当文件大于指定的大小后,删除文件前几行继续存储

该代码段展示了如何在Android中实现日志管理,包括启动日志记录,停止日志记录,以及当日志文件大小超过4MB时删除部分行。使用`logcat`命令收集系统日志,并将其写入文件,同时提供了移除文件顶部行数的功能。
摘要由CSDN通过智能技术生成
public class LogFileManager {
    public static volatile boolean running = true;
    public static Process process;
    public static int logFileSize = 4*1024*1024;//4MB
    public static InputStream is;
    public static FileOutputStream outputStream;

    public static void startLog(){
        new Thread("UploadLogFile") {
            @Override
            public void run() {
                super.run();
                while (running) {
                    try {
                        String path = HwContext.getContext().getFilesDir().getPath() + "/"
                                + Event.fileName;
                        Runtime.getRuntime().exec("logcat -c");
                        String[] cmds = new String[]{"logcat", "*:w"};
                        File file = new File(path);
                        process = Runtime.getRuntime().exec(cmds);
                        is = process.getInputStream();
                        outputStream = new FileOutputStream(file);
                        int len = 0;
                        byte[] buf = new byte[1024];
                        while (file.length() < logFileSize && (-1 != (len = is.read(buf)))) {
                            outputStream.write(buf, 0, len);
                        }
                        if (file.length() > logFileSize) {
                            removeLines(file,100);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();

                    }
                }
            }
        }.start();

    }

    public static void stopLog(){
        running = false;
        process.destroyForcibly();
        try {
            if (is!=null){
                is.close();
            }
            if (outputStream!=null){
                outputStream.close();
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static void removeLines(File file, int lineNum) throws IOException{
        RandomAccessFile raf = null;
        try{
            raf = new RandomAccessFile(file, "rw");
            long writePosition = raf.getFilePointer();//文件起始位置
            for (int i = 0 ; i < lineNum ; i++){
                String line = raf.readLine();
                if(line == null){
                    break;
                }
            }
            // Shift the next lines upwards.
            long readPosition = raf.getFilePointer();//跳过几行后的位置

            byte[] buff = new byte[1024];
            int n;
            while (-1 != (n = raf.read(buff))) {
                raf.seek(writePosition);
                raf.write(buff, 0, n);
                readPosition += n;
                writePosition += n;
                raf.seek(readPosition);
            }
            //raf.setLength(writePosition);
        } catch(IOException e){
            LogUtils.e(e.toString());
        } finally{
            try{
                if(raf != null){
                    raf.close();
                }
            }catch(IOException e){
                LogUtils.e(e.toString());
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值