目录
手机用户请
横屏
获取最佳阅读体验,REFERENCES
中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。
平台 | 地址 |
---|---|
CSDN | https://blog.csdn.net/sinat_28690417 |
简书 | https://www.jianshu.com/u/3032cc862300 |
个人博客 | https://yiyuery.club |
工具类
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: https:yiyuery.club
* @date: 2019/5/20 20:57
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/
package com.example.dependency.io;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019-06-06 20:57
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019-06-06
* @modify reason: {方法名}:{原因}
* ...
*/
@Slf4j
@Data
public class ReaderAndWrite {
private InputStreamReader reader;
private OutputStreamWriter writer;
private final static ReaderAndWrite readerAndWrite = new ReaderAndWrite();
/**
* 初始化 从指定文件读的 Reader
*
* @param source
*/
public void read(File source) {
if (!source.exists()) {
throw new IllegalArgumentException("file be read can't be null");
}
try {
FileInputStream fip = new FileInputStream(source);
//解决读乱码
setReader(new InputStreamReader(fip, "UTF-8"));
} catch (Throwable e) {
log.error("read file catch exception", e);
}
}
/**
* 初始化写到指定文件的 Writer
*
* @param target
*/
public void write(File target) {
try {
if (!target.exists()) {
target.createNewFile(); // 创建新文件
}
//解决写乱码
setWriter(new OutputStreamWriter(new FileOutputStream(target), "UTF-8"));
} catch (Throwable e) {
log.error("write file catch exception", e);
}
}
/**
* 递归读取待写入的文件
* 遍历根目录下所有文件
* @param root
* @param others
* @return
*/
public List<File> addFilesToHolder(File root, File... others) {
List<File> holder = Objects.nonNull(others) && others.length > 0 ? new ArrayList<>(Arrays.asList(others)) : Lists.newArrayList();
if (root.isDirectory()) {
for (File f : Objects.requireNonNull(root.listFiles())) {
holder.addAll(ListUtils.emptyIfNull(addFilesToHolder(f)));
}
} else {
holder.add(root);
}
return holder;
}
public static void main(String[] args) {
String rootPath = "/Users/xiazhaoyang/Capsule/workspace/ishare-project/common-dependencies";
String[] modules = new String[]{"common-dependency", "common-template"};
Arrays.asList(modules).forEach(module->{
List<File> allFiles = readerAndWrite.addFilesToHolder(
Paths.get(rootPath,module,"/src/main").toFile(),
Paths.get(rootPath,module,module+".md").toFile(),
Paths.get(rootPath,module,module+".gradle").toFile());
doCombineTask(allFiles,module);
});
//关闭文件流
try{
readerAndWrite.getWriter().close();
readerAndWrite.getReader().close();
}catch (Throwable e){
log.error("close file stream failed!");
}
}
/**
* 开始合并文件
* @param allFiles
* @param module
*/
private static void doCombineTask(List<File> allFiles, String module) {
if(CollectionUtils.isNotEmpty(allFiles)){
allFiles.forEach(e->{
log.info("> "+e.getName());
});
}
readerAndWrite.write(Paths.get("/Users/xiazhaoyang/Documents/coding",module+".md").toFile());
for (File file : allFiles) {
try {
readerAndWrite.read(file);
readerAndWrite.markdown("> "+file.getName());
readerAndWrite.markdown("```java");
StringBuffer sb = new StringBuffer();
while (readerAndWrite.getReader().ready()) {
// 转成char加到StringBuffer对象中
sb.append((char)readerAndWrite.getReader().read());
}
readerAndWrite.getWriter().write(sb.toString());
readerAndWrite.markdown("```");
readerAndWrite.getWriter().flush();
}catch (Throwable e){
log.error("read for write error",e);
}
}
}
/**
* 添加markdown标记
* @param tag
* @throws Exception
*/
private void markdown(String tag) throws Exception{
// \r\n即为换行
readerAndWrite.getWriter().write("\r\n");
readerAndWrite.getWriter().write(tag);
readerAndWrite.getWriter().write("\r\n");
}
}
效果
22:04:12.400 [main] INFO com.example.dependency.io.ReaderAndWrite - > common-dependency.md
22:04:12.406 [main] INFO com.example.dependency.io.ReaderAndWrite - > common-dependency.gradle
22:04:12.406 [main] INFO com.example.dependency.io.ReaderAndWrite - > log4j.properties
22:04:12.406 [main] INFO com.example.dependency.io.ReaderAndWrite - > ReaderAndWrite.java
22:04:12.429 [main] INFO com.example.dependency.io.ReaderAndWrite - > common-template.md
22:04:12.430 [main] INFO com.example.dependency.io.ReaderAndWrite - > common-template.gradle
22:04:12.430 [main] INFO com.example.dependency.io.ReaderAndWrite - > HelloController.java
更多
扫码关注“架构探险之道”,获取更多源码和文章资源
知识星球(扫码加入获取源码和文章资源链接)