Need some help in solving the problem. There is a text document in which i want to find words qwee, deff and add the result (qwee = 12130, deff = 110). The result is save to the another txt file. Which way should I use?
--------- 2016-08-08 12:32:52 GMT+03:00 S03 --------- Mii::QW,; LOADING PROGRAM MM S03 2016-08-08 12:32:50 NAME INDEX COUNT qwee 1 : 12130 Ssss 2 : 10 deff 3 : 110
Total: 2333903 COMMAND EXECUTED
Please try this example and see if it solves your problem. I assume you keep this file
data.txt
--------- 2016-08-08 12:32:52 GMT+03:00 S03 --------- Mii::QW,; LOADING PROGRAM MM S03 2016-08-08 12:32:50 NAME INDEX COUNT qwee 1 : 12130 Ssss 2 : 10 deff 3 : 110
fede 4 : 10
Total: 2333903 COMMAND EXECUTED
Then we run the program
import java.io.*;;
import java.util.Scanner;
class Main {
public static void main(String args[]) {
try {
String content = new Scanner(new File("data.txt")).useDelimiter("\\Z").next();
String split[] = content.split("( )|(\n)");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("output.txt"), "utf-8"))) {
for (int i = 0; i < split.length; i++) {
if (split[i].equals("qwee")) {
writer.write("qwee " + split[i + 3] + "\n");
}
if (split[i].equals("deff")) {
writer.write("deff " + split[i + 3] + "\n");
} ;
}
writer.close();
} catch (Exception e) {
e.printStackTrace(); ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
It created a new file with the data you want
output.txt
qwee 12130
deff 110
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.