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;
}
}
java 读取txt按某字符拆分修改内容
最新推荐文章于 2023-04-12 21:27:24 发布