package com.example.lib_tab;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyClass {
private static String targetFilePath = "C:\\Users\\HASEE\\Desktop\\Downloads\\clean3";
private static String copyFilePath = "C:\\Users\\HASEE\\Desktop\\Downloads\\total.json";
public static void main(String[] args) {
//定义输出目录
// String copyFilePath="E:\\Mycode\\SBgong\\output\\1.txt";
int fileCount = 0;
int folderConut = 0;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(copyFilePath));
File[] list = new File(targetFilePath).listFiles();
for (File file : list) {
if (file.isFile()) {
fileCount++;
file.getAbsolutePath();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line + ",");
bw.newLine();
}
br.close();
} else {
folderConut++;
}
}
bw.close();
String content = readFileContent(copyFilePath);
content="["+content.substring(0,content.length()-1)+"]";
JSONArray jsonArray = JSONObject.parseArray(content);
// system cache apk residual
Object o = jsonArray.getJSONObject(0);
for (int i=0;i<jsonArray.size();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
JSONArray systemArray = jsonObject.getJSONArray("system");
JSONArray cacheArray = jsonObject.getJSONArray("cache");
JSONArray apkArray = jsonObject.getJSONArray("apk");
JSONArray residualArray = jsonObject.getJSONArray("residual");
}
// jsonObject.getString("");
System.out.println("jsonArray.size:"+jsonArray.size());
} catch (Exception e) {
}
}
/**
* 获取文件中文本内容
* @param fileName 文件路劲名
* @return
*/
public static String readFileContent(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sbf.toString();
}
}
/**
*
*追加写入
*/
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File("E:\\dd.txt");
fw = new FileWriter(f, true);
PrintWriter pw = new PrintWriter(fw);
pw.println("追加内容");
pw.flush();
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
******正常写:
private static final String save_path="C:\\Users\\HASEE\\Desktop\\广告sdk\\log.txt";
/**
* @param content 保存字符串
*/
public static void saveContent(String content){
try {
File f = new File(save_path);
FileWriter fw = new FileWriter(f, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}