java分隔log文件日志文件(并将代码打包成exe可执行文件)

先写java代码逻辑,然后打成jar包,可以用exe4j将jar包打包成exe可执行文件

exe4j打包软件下载地址:

链接: https://pan.baidu.com/s/1nMOyNVRS6PjudTOFr6zGIQ 提取码: cntf 

详细可参照教程:https://blog.csdn.net/lxw1005192401/article/details/81557537

 

java代码如下:

package Files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import javax.swing.JFileChooser;
/**
 * @author boveysmith
 * @date 2019/12/19
 * @version V1.0
 */
public class FileSplit {

    /**
     * 实现对大文件的切割与合并。 按指定个数切(如把一个文件切成10份)或按指定大小切(如每份最大不超过10M),这两种方式都可以。
     */
    public static void main(String[] args) {
        JFileChooser jfc = new JFileChooser();// Swing中的选择文件
        // 选择文件
        String lastPath = Registery.getInstance().getValue("openPath");
        if(lastPath != null) {
            jfc.setCurrentDirectory(new File(lastPath));
        } 
        int result = jfc.showOpenDialog(null);// 显示框架用于选择文件
        File file = null;// 要切割的文件
        File dest = null;// 目的地文件
        try {
            if (result == JFileChooser.APPROVE_OPTION) {// 选中文件
                // 切割文件
                file = jfc.getSelectedFile();// 用户选择的文件
                String path = file.getParent();
                Registery.getInstance().writeValue("openPath", path);
                dest = new File(file.getParent(), "spliFile");
                cutingFile(file, dest);// 切割方法
                // 2合并(运行时,直接对刚才切割的那些文件碎片进行合并)
//                String fileName = file.getName();
//                mergeDemo(dest, fileName);// 合并文件
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void mergeDemo(File dest, String fileName) throws IOException {
        // 健壮性防护(用File对象去开道)
        if (!dest.exists()) {
            throw new RuntimeException("文件不存在");
        }
        // 用一个文件数组将里面的文件都装起来
        File parth[] = dest.listFiles();// 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
        if (parth.length == 0) {
            throw new RuntimeException("碎片不存在");
        }
        // y用序列流来合并
        ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
        // for (int i = 0; i < parth.length; i++) {
        // list.add(new FileInputStream(parth[i]));//不能这样,这样合并出来的文件是顺序乱的
        // }
        for (int i = 0; i < parth.length; i++) {
            list.add(new FileInputStream(new File(dest, fileName + (i + 1) + "part")));// 套接技术,文件加的顺序要和原文件一样
        }
        // 枚举对象接口
        Enumeration<FileInputStream> en = Collections.enumeration(list);
        SequenceInputStream sq = new SequenceInputStream(en);
        // 写入到新文件中
        FileOutputStream fou = new FileOutputStream(new File(dest, fileName));
        byte buf[] = new byte[1024];
        sq.read(buf);
        int len = 0;
        while ((len = sq.read(buf)) > 0) {
            fou.write(buf, 0, len);
        }
        fou.close();
        sq.close();
    }

    private static void cutingFile(File source, File dest) {
        // 切割
        try {
            FileInputStream fis = new FileInputStream(source);
            if (!dest.exists()) {// 文件操作IO流要判断文件是否存在。
                dest.mkdir();
            }
            byte buf[] = new byte[1024 * 1024 * 10];// 10M
            int len = 0;
            int cout = 1;
            while ((len = fis.read(buf)) != -1) {
                // 用OUT流来切割文件
                String targetName = source.getName();
                if (targetName.indexOf(".") != -1) {
                    targetName = targetName.substring(0, targetName.indexOf(".")) + (cout++)
                            + targetName.substring(targetName.indexOf("."), targetName.length());
                } else {
                    targetName = source.getName() + (cout++);
                }
                FileOutputStream fout = new FileOutputStream(new File(dest, targetName));
                fout.write(buf, 0, len);
                fout.close();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
javaSwing自带文件选择框,使其记住上一次打开目录可将路径写入注册表,代码如下:

/**
 * This software code is only used for the authorized 
 * person to maintain and two development. It is not 
 * allowed to be issued to the outside world. It is 
 * necessary to strictly observe the confidentiality 
 * agreement we have signed.
 * copyright by 1sdk.cn
 *
 */
package Files;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * @author boveysmith
 * @date 2019/12/19
 * @version V1.0
 */
public class Registery {
    private static Registery instance = null;
    private Registery() {}
    synchronized public static Registery getInstance() {
        if (instance == null) {
            instance = new Registery();
        }
        return instance;
    }

    // 把相应的值储存到变量中去
    public void writeValue(String[] keys, String[] values) {
        if (keys.length > values.length)
            return;
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表myprefer.(myprefer如果大些,注册表中会加“/”)
        Preferences pre = Preferences.systemRoot().node("/myprefer");
        for (int i = 0; i < keys.length; i++) {
            pre.put(keys[i], values[i]);
        }
    }

    public void writeValue(String key, String value) {
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表myprefer
        Preferences pre = Preferences.systemRoot().node("/myprefer");
        pre.put(key, value);
    }

    /***
     * 
     * 根据key获取value
     *
     */
    public String getValue(String key) {
        Preferences pre = Preferences.systemRoot().node("/myprefer");
        return pre.get(key, null);
    }

    /***
     * 
     * 清除注册表
     *
     * @throws BackingStoreException
     */
    public void clearValue() throws BackingStoreException {
        Preferences pre = Preferences.systemRoot().node("/myprefer");
        pre.clear();
    }

//    public static void main(String[] args) throws BackingStoreException {
//        String[] keys = { "version", "initial", "creator" };
//        String[] values = { "1.3", "ini.mp3", "caokai1818@sina.com" };
//        Registery.getInstance().writeValue(keys, values);
//        System.out.println(Registery.getInstance().getValue("version"));
//        // 可以读取任意路径下的
//        try {
//            Process ps = null;
//            ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\myprefer");
//            ps.getOutputStream().close();
//            InputStreamReader i = new InputStreamReader(ps.getInputStream());
//            String line;
//            BufferedReader ir = new BufferedReader(i);
//            while ((line = ir.readLine()) != null) {
//                System.out.println(line);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值