当前位置:   article > 正文

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

android 多个文件写入一个文件
  1. package com.example.lib_tab;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. public class MyClass {
  13. private static String targetFilePath = "C:\\Users\\HASEE\\Desktop\\Downloads\\clean3";
  14. private static String copyFilePath = "C:\\Users\\HASEE\\Desktop\\Downloads\\total.json";
  15. public static void main(String[] args) {
  16. //定义输出目录
  17. // String copyFilePath="E:\\Mycode\\SBgong\\output\\1.txt";
  18. int fileCount = 0;
  19. int folderConut = 0;
  20. try {
  21. BufferedWriter bw = new BufferedWriter(new FileWriter(copyFilePath));
  22. File[] list = new File(targetFilePath).listFiles();
  23. for (File file : list) {
  24. if (file.isFile()) {
  25. fileCount++;
  26. file.getAbsolutePath();
  27. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
  28. String line;
  29. while ((line = br.readLine()) != null) {
  30. bw.write(line + ",");
  31. bw.newLine();
  32. }
  33. br.close();
  34. } else {
  35. folderConut++;
  36. }
  37. }
  38. bw.close();
  39. String content = readFileContent(copyFilePath);
  40. content="["+content.substring(0,content.length()-1)+"]";
  41. JSONArray jsonArray = JSONObject.parseArray(content);
  42. // system cache apk residual
  43. Object o = jsonArray.getJSONObject(0);
  44. for (int i=0;i<jsonArray.size();i++){
  45. JSONObject jsonObject=jsonArray.getJSONObject(i);
  46. JSONArray systemArray = jsonObject.getJSONArray("system");
  47. JSONArray cacheArray = jsonObject.getJSONArray("cache");
  48. JSONArray apkArray = jsonObject.getJSONArray("apk");
  49. JSONArray residualArray = jsonObject.getJSONArray("residual");
  50. }
  51. // jsonObject.getString("");
  52. System.out.println("jsonArray.size:"+jsonArray.size());
  53. } catch (Exception e) {
  54. }
  55. }
  56. /**
  57. * 获取文件中文本内容
  58. * @param fileName 文件路劲名
  59. * @return
  60. */
  61. public static String readFileContent(String fileName) {
  62. File file = new File(fileName);
  63. BufferedReader reader = null;
  64. StringBuffer sbf = new StringBuffer();
  65. try {
  66. reader = new BufferedReader(new FileReader(file));
  67. String tempStr;
  68. while ((tempStr = reader.readLine()) != null) {
  69. sbf.append(tempStr);
  70. }
  71. reader.close();
  72. return sbf.toString();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. } finally {
  76. if (reader != null) {
  77. try {
  78. reader.close();
  79. } catch (IOException e1) {
  80. e1.printStackTrace();
  81. }
  82. }
  83. }
  84. return sbf.toString();
  85. }
  86. }
  87. /**
  88. *
  89. *追加写入
  90. */
  91. public void method1() {
  92. FileWriter fw = null;
  93. try {
  94. //如果文件存在,则追加内容;如果文件不存在,则创建文件
  95. File f=new File("E:\\dd.txt");
  96. fw = new FileWriter(f, true);
  97. PrintWriter pw = new PrintWriter(fw);
  98. pw.println("追加内容");
  99. pw.flush();
  100. fw.flush();
  101. pw.close();
  102. fw.close();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }

******正常写:

  1. private static final String save_path="C:\\Users\\HASEE\\Desktop\\广告sdk\\log.txt";
  2. /**
  3. * @param content 保存字符串
  4. */
  5. public static void saveContent(String content){
  6. try {
  7. File f = new File(save_path);
  8. FileWriter fw = new FileWriter(f, true);
  9. PrintWriter pw = new PrintWriter(fw);
  10. pw.println(content);
  11. pw.flush();
  12. fw.flush();
  13. pw.close();
  14. fw.close();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号