JavaSwing版本的日志文件分割器

功能:

分割比100M还大的日志为多个100M的左右的小日志

上效果图

使用方法

点击打开按钮 打开一个log文件,然后点击切割按钮

直接上代码

package com.liyghting.txtsplit;

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.OutputStream;

import java.nio.file.FileSystem;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

/**

* @author LiYiGuang

* @date 2021/1/8 15:37

*/

public class TxtSplitSwing {

public static void main(String[] args) throws Exception {

final JFrame jf = new JFrame("日志分割工具窗口");

jf.setSize(400, 250);

jf.setLocationRelativeTo(null);

jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

// 创建文本区域, 用于显示相关信息

final JTextArea msgTextArea = new JTextArea(10, 30);

msgTextArea.setLineWrap(true);

panel.add(msgTextArea);

JButton openBtn = new JButton("打开");

openBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

showFileOpenDialog(jf, msgTextArea);

}

});

panel.add(openBtn);

JButton saveBtn = new JButton("分割");

saveBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

try {

showFileSaveDialog(jf, msgTextArea);

} catch (Exception exception) {

exception.printStackTrace();

msgTextArea.setText("切割异常" + exception.getMessage());

}

}

});

panel.add(saveBtn);

jf.setContentPane(panel);

jf.setVisible(true);

}

/*

* 打开文件

*/

private static void showFileOpenDialog(Component parent, JTextArea msgTextArea) {

msgTextArea.setText(null);

// 创建一个默认的文件选取器

JFileChooser fileChooser = new JFileChooser();

// 设置默认显示的文件夹为当前文件夹

fileChooser.setCurrentDirectory(new File("D:\\user\\01370744\\Downloads"));

// 设置文件选择的模式(只选文件、只选文件夹、文件和文件均可选)

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

// 设置是否允许多选

fileChooser.setMultiSelectionEnabled(false);

// 添加可用的文件过滤器(FileNameExtensionFilter 的第一个参数是描述, 后面是需要过滤的文件扩展名 可变参数)

// fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("zip(*.zip,

// *.rar)", "zip", "rar"));

// 设置默认使用的文件过滤器

fileChooser.setFileFilter(new FileNameExtensionFilter("txt(*.txt, *.log)", "txt", "log"));

// 打开文件选择框(线程将被阻塞, 直到选择框被关闭)

int result = fileChooser.showOpenDialog(parent);

if (result == JFileChooser.APPROVE_OPTION) {

// 如果点击了"确定", 则获取选择的文件路径

File file = fileChooser.getSelectedFile();

// 如果允许选择多个文件, 则通过下面方法获取选择的所有文件

// File[] files = fileChooser.getSelectedFiles();

msgTextArea.append("打开文件: " + file.getAbsolutePath());

}

}

/*

* 选择文件保存路径

*/

private static void showFileSaveDialog(Component parent, JTextArea msgTextArea) throws Exception {

String txtPath = msgTextArea.getText().substring(6);

Path path = Paths.get(txtPath);

long currentTimeMillis = System.currentTimeMillis();

long houserend = 1024L * 1024 * 100;

byte[] bytes = Files.readAllBytes(path);

FileSystem fileSystem = path.getFileSystem();

String separator = fileSystem.getSeparator();

int lastIndexOf = txtPath.lastIndexOf(separator);

String dir = txtPath.substring(0, lastIndexOf);

String lastFile = txtPath.substring(lastIndexOf + 1, txtPath.lastIndexOf("."));

String smallFile = dir + separator + currentTimeMillis + lastFile + "_1.txt";

msgTextArea.append("\n" + "切割输出文件开始");

msgTextArea.append("\n" + smallFile);

Path smallPath = Paths.get(smallFile);

Files.createFile(smallPath);

OutputStream outputStream = Files.newOutputStream(smallPath);

byte[] temp = new byte[4096];

int length = 4096;

int writedSize = 0;

int writedFileCount = 1;

for (int i = 0; i < bytes.length;) {

if (i + 4096 > bytes.length) {

length = bytes.length - i;

temp = new byte[length];

}

System.arraycopy(bytes, i, temp, 0, length);

outputStream.write(temp);

writedSize = writedSize + length;

if (writedSize > houserend) {

writedFileCount++;

smallFile = dir + separator + currentTimeMillis + lastFile +"_"+ writedFileCount + ".txt";

msgTextArea.append("\n" + smallFile);

smallPath = Paths.get(smallFile);

Files.createFile(smallPath);

outputStream = Files.newOutputStream(smallPath);

writedSize = 0;

}

if (i + 4096 > bytes.length) {

break;

} else {

i = i + 4096;

}

}

msgTextArea.append("\n" + "切割输出文件结束,cost:" + (System.currentTimeMillis() - currentTimeMillis) / 1000 + "秒");

}

}

java日志切割工具_JavaSwing版本的日志文件分割器相关推荐

  1. java日志切割工具_用 Java 实现的日志切割清理工具

    对于服务器的日常维护来说,日志清理是非常重要的事情,如果残留日志过多则严重浪费磁盘空间同时影响服务的性能.如果用手工方式进行清理,会花费太多时间,并且很多时候难以满足实际要求.例如:如何在每个星期六凌 ...

  2. Linux日志切割工具logrotate原理和配置详解

     前言: 对于Linux系统安全来说,日志文件是及其重要的工具 一.logrotate概念 1.1 概念 logrotate 程序是一个日志文件管理工具.用于分割日志文件,删除旧的日志文件,并创建新的 ...

  3. centos 日志切割_Linux 日志切割工具cronolog详解

    一.前言 二.cronolog 简介 三.cronolog 特点 四.cronolog 安装 五.cronolog 使用 六.cronolog 总结 注,操作系统 CentOS 6.4 x86_64, ...

  4. Linux日志分割软件,Linux 日志切割工具cronolog

    一.前言 二.cronolog 简介 三.cronolog 特点 四.cronolog 安装 五.cronolog 使用 六.cronolog 总结 注,操作系统 CentOS 6.4 x86_64, ...

  5. Linux日志切割工具cronolog详解

    一.前言 大家都知道apache服务器,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计.本博文主要讲解Web服务器日志切割工具cronolog,下面我们就来详细的讲解一下. 二.cr ...

  6. 日志切割工具cronolog详解

    日志切割工具cronolog详解 大家都知道apache服务器,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计.本文主要讲解Web服务器日志切割工具cronolog,下面我们就来详细 ...

  7. nginx日志切割并使用flume-ng收集日志

    nginx的日志文件没有rotate功能.如果你不处理,日志文件将变得越来越大,还好我们可以写一个nginx日志切割脚本来自动切割日志文件. 第一步就是重命名日志文件,不用担心重命名后nginx找不到 ...

  8. java 使用itextpdf工具实现HTML转PDF文件

    java 使用itextpdf工具实现HTML转PDF文件 本文转载https://my.oschina.net/960823/blog/1588166内容 demo maven依赖包 <dep ...

  9. java 文件分割器_Java分割器

    java 文件分割器 Java Spliterator is one of the four iterators – Enumeration, Iterator, ListIterator and S ...

最新文章

  1. 她穿着自己用 17 封拒信做成的裙子,参加了博士论文答辩...
  2. Knockout应用开发指南(完整版) 目录索引
  3. 用g.raphael.js高速绘制饼图、柱状图、点状图、折线图(下)
  4. 双系统安装 Windows8和Windows Server2012
  5. 推荐ApacheCN开源的一个机器学习路线图
  6. 大数据发行版本+组件中的竞品/等同地位关系(持续更新中)
  7. Use function as controller
  8. 最后一天,特邀小姐姐配音拉票,今日可投28票
  9. LeetCode 03. 无重复字符的最长子串
  10. Emlog文章海报插件
  11. java与微信小程序通讯_java与微信小程序实现websocket长连接
  12. 5.10 Memory Networks 记忆网络的应用与方法
  13. 读书札记:瑞士法郎的因素
  14. 三菱q系列plc 和电脑socket_组态王和三菱Q系列PLC以太网通信
  15. TRIMMEAN函数实例:评委打分计算最后得分
  16. CSS基础-09-布局(定位 position、浮动float,元素对其、图像对其、文本对齐、元素内内容对齐,元素堆叠)
  17. 真正的人品,藏在对弱者的态度里
  18. 学法减分助手PRO小程序源码
  19. 手游族迎来360手机卫士神助攻 玩家免打扰尽情发育
  20. linux peek,Peek - Gif 录制软件

热门文章

  1. USB HOST芯片SL2.1A调试心得
  2. 谷歌查看html地址_104篇CVPR 2019论文,追踪谷歌、Facebook、英伟达研究课题
  3. 计算机课学生段密码,优课互联课堂学生端
  4. 分享五年码农生涯历程经验及2018总结 | 掘金年度征文
  5. shell编程发送按键
  6. 2021.5.16数仓之用户画像表模型设计
  7. android auto三方地图,GitHub - puderty/pudev: 1,百度CarLife映射与高清修改,纯属自用。2,Android Auto的第三方地图...
  8. 【分布式计算:原理、算法和系统】第二章 分布式计算模型
  9. pyqt5可以写手机apk吗_不需要写代码,直接在unity实现手机可以跑的毛发效果
  10. ASP.NET MVC程序设计实验一:布局页和主页设计