java 读取txt按某字符拆分修改内容

public boolean execute() {

		try {

			String oldtxt = "test.txt"
			String newtxt1 = "new1.txt";
			String newtxt2 = "new2.txt";
			
			// 新保存存储路径
			String LocDir = "E://";
			// 原始test存储路径
			String FromDir = "E://";


			/** read txt */
			String pathname = FromDir + oldtxt;
			File filename = new File(pathname);// read above path .txt file
			InputStreamReader reader = new InputStreamReader(new FileInputStream(filename), "GBK");

			BufferedReader br = new BufferedReader(reader);

			// 定义输出
			StringBuilder outTxt1 = new StringBuilder();
			StringBuilder outTxt2 = new StringBuilder();

			String line = "";
			line = br.readLine();

			
			String tmp = "";
			String trandata[] = new String[17];

			byte[] utfbyte = line.getBytes("UTF-8");
			tmp = new String(utfbyte, "UTF-8");
			trandata = tmp.split("\\|");
			int count = trandata.length;


			while ((line = br.readLine()) != null) {

				utfbyte = line.getBytes("UTF-8");
				tmp = new String(utfbyte, "UTF-8");
				trandata = tmp.split("\\|");
				count = trandata.length;

				// 取第11列
				String paymentcode = trandata[10].replace(" ", "");
				
				//判断是否等于hello
				if (paymentcode.equals("hello")) {
					outTxt1.append(line); 
					outTxt1.append("\r\n");
				} else {
					outTxt2.append(line); 
					outTxt2.append("\r\n");
				}
			}

			 br.close();
			 reader.close();

			 File writename1 = new File(LocDir + newtxt1);
			 File writename2 = new File(LocDir + newtxt2);
			 writename1.createNewFile();
			 writename2.createNewFile();
			 BufferedWriter out1 = new BufferedWriter(new FileWriter(writename1));
			 BufferedWriter out2 = new BufferedWriter(new FileWriter(writename2));
			 out1.write(outTxt1.toString());
			 out2.write(outTxt2.toString());
			 out1.flush();
			 out2.flush();
			 out1.close();
			 out2.close();
			 return true;
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值