java实现多个.json文件整和成一个json文件或者txt文件

package com.herdsric.crm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class ReadFromFile {
 
    /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName, String writefileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
 
                appendMethodB(writefileName, tempString + "\r\n");
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
 
    /**
     * 递归讀取目录下的所有文件及子目录下所有文件
     */
    public static void findDir(String patn, String writefileName) {
        File dir = new File(patn);
 
        if (dir.isDirectory()) {
            System.out.println("获取文件夹下的集合");
            String[] children = dir.list();
            // 递归读取目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                String chidPaath = patn + '/' + children[i];
                File child = new File(chidPaath);
                if (child.isDirectory()) {
                    System.out.println("文件夹");
                    findDir(chidPaath, writefileName);
                } else {
                    System.out.println("文件-----讀取" + child.getName());
                    readFileByLines(chidPaath, writefileName);
                }
            }
        } else {
            readFileByLines(patn, writefileName);
        }
    }
 
    /**
     * B方法追加文件:使用FileWriter
     */
    public static void appendMethodB(String fileName, String content) {
        try {
            // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        String path = "E:\\a";// 选择你目标文件夹(所有需要整和的json文件)
        String writeFile = "E:\\json.txt";//选择你要生成的文件
        File file = new File(writeFile);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        ReadFromFile.findDir(path, writeFile);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值