Android 多个文件写入一个文件,读取文件中的内容,追加写入;正常写file

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();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值