(四)Java读写文件,合并成新的文件

时间:2022-07-25
本文章向大家介绍(四)Java读写文件,合并成新的文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文件一:

AppCrashed participate__recharge_activity participate_activity

AppClickAppViewScreen WebClickWebStay PlanMsgSendPrepare PlanConverted AppEnd

文件二:

App 崩溃 参与充值活动 参与活跃活动 App 元素点击 App 浏览页面 Web 元素点击 Web 视区停留 Web 浏览页面 同时在线人数(特殊心跳事件) 拍卖行交易买入 装备升级 好感度培养 道具商店点击 卡牌抽取 分享 剧情 钻石商店点击 PVP战斗 关卡PVE战斗 道具商店提交订单 道具商店支付订单 神魄升级(属性系统) 新手引导通过 角色升级 登录游戏 创建角色 道具产出 道具消耗 订单提交【钻石礼包】 订单购买【钻石礼包】 卡牌升级 完成日常任务 Push 点击 App 激活 注册账号 登陆账号 消息已准备 消息已发送 推送转化 App启动 $退出APP

文件读取工具:

package xxxx.com.test.file_acc; import java.io.*;

public class FileUtils { public static void readToBuffer(StringBuffer buffer,String filePath) throws IOException { InputStream is = new FileInputStream(filePath); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = reader.readLine(); while (line != null){ // line为空说明读完了 // 将读到的内容添加到buffer中 buffer.append(line); buffer.append("n"); // 读取下一行 line = reader.readLine(); } reader.close(); is.close(); }

public static String readFile(String filePath) throws IOException {
    StringBuffer sb = new StringBuffer();
    // 传入buffer和文件路径
    FileUtils.readToBuffer(sb,filePath);
    return sb.toString();
}

}

文件合并,执行:

package xxxx.com.test.file_acc; import java.io.*;

public class File_append { public static void main(String[] args) throws IOException { String filePath01 = "F:ProjectWordCountsrcmainjavaxxxxcomtestfile_accfile01"; String readFile01 = FileUtils.readFile(filePath01); String filePath02 = "F:ProjectWordCountsrcmainjavaxxxxcomtestfile_accfile02"; String readFile02 = FileUtils.readFile(filePath02);

    System.out.println(readFile01);
    System.out.println("n-------------------n");
    System.out.println(readFile02);
    System.out.println("n-------------------n");
    // 进行文件合并
    mergeFiles(readFile01,readFile02);

    String file03 = "F:\Project\WordCount\file03.txt";
    String read = FileUtils.readFile(file03);
    System.out.println(read);
}


public static void mergeFiles(String br1, String br2) throws IOException {
    // 写入的路径
    PrintWriter pw = new PrintWriter("file03.txt");

    // 用数组将读取的数据进行切分
    String[] split1 = br1.split("n");
    String[] split2 = br2.split("n");
    for (int i = 0; i < split1.length; i++) {
        // 开始将两个文件的数据写入到一起
        pw.println(split1[i]+"t"+split2[i]);
        // 每行刷新
        pw.flush();
    }
    pw.close();
}

}

输出结果:

AppCrashed App 崩溃 participate__recharge_activity 参与充值活动 participate_activity 参与活跃活动

AppViewScreen App 浏览页面

WebStay Web 视区停留

PlanMsgSendPrepare 消息已准备

PlanConverted 推送转化

AppEnd $退出APP