把多个.json⽂件合成⼀个⽂件1.⾸先把所有json放在⼀个⽂件夹下,
path:⽂件夹的路径⽂件夹下放置多个json⽂件
writeFile :你要⽣成的⽂件
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 = adLine()) != 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()) {
if (child.isDirectory()) {
System.out.println("⽂件夹");
mysql下载下来是一个文件夹
findDir(chidPaath, writefileName);
} else {
System.out.println("⽂件-----讀取" + Name());
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 = "C:\\Users\\Administrator\\Desktop\\data_location-master";// 选择你的⽂件        String writeFile = "D:\\";//选择你要⽣成的⽂件
File file = new File(writeFile);
if (!ists()) {
try {
} catch (IOException e) {
e.printStackTrace();
}
}
ReadFromFile.findDir(path, writeFile);
}
}
以下成功案例,执⾏成功点个赞谢谢

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。