Java 二进制处理

111 篇文章 0 订阅
package com.xiuye.util.merge;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import com.xiuye.util.Pointer;
import com.xiuye.util.log.XLog;

public class MergeFileByList {

	private static final String ROOT_PATH = "C:/download";

	public static void mergeFile(Path path) throws IOException {
		
		Path outputPath = Paths.get(ROOT_PATH + "/" + "output/" + path.getFileName() + ".coff");
		if(!Files.exists(outputPath)) {
			Path parentPath = outputPath.getParent();
			if (Objects.nonNull(parentPath)) // null will be returned if the path has no parent
			{
				Files.createDirectories(parentPath);
			}
//			Files.createDirectories(outputPath);
			Files.createFile(outputPath);
		}
		OutputStream out = Files.newOutputStream(outputPath);
		
		Pointer.of(Files.newBufferedReader(path)).ifPresentV(reader -> {
			String dataLine = null;
			try {
				while ((dataLine = reader.readLine()) != null) {

					if (!dataLine.isEmpty() && !dataLine.matches("^$(.*")) {
						String patchFile = dataLine.replace("/", ROOT_PATH);
						Pointer.of(Files.newInputStream(Paths.get(patchFile))).ifPresentV(stream -> {
							byte[] data = new byte[1024 * 4];
							int len = -1;
							try {
								XLog.lg(patchFile, stream.available());
								while ((len = stream.read(data, 0, 1024 * 4)) != -1) {
//											XLog.lg(new String(data,StandardCharsets.UTF_8));
									appendDataToFile(out, data, 0, len);
								}
							} catch (IOException e) {
								e.printStackTrace();
							} finally {
								try {
									stream.close();
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						});
					}

				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (Objects.nonNull(reader)) {
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(Objects.nonNull(out)) {
					try {
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}

	private static void appendDataToFile(OutputStream out, byte[] data, int offset, int len) throws IOException {
		try {
			out.write(data, offset, len);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static final void appendDataToFile(byte[] data, String file) {

	}

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

		Files.list(Paths.get(ROOT_PATH)).forEach(path -> {
			XLog.lg(Files.isDirectory(path), path);
			if (!Files.isDirectory(path)) {
				XLog.lg("Open", path);
				try {
					mergeFile(path);
				} catch (IOException e) {
					e.printStackTrace();
				}
				XLog.lg("Over", path);
			}
		});

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值