[英]Replace a string with some part of a text file
我想在文件中找到一個帶編號的模式(我成功完成),通過使用類中的另一個方法(我也成功地將其轉換為我想要的數字格式)...
我的問題是我想用以前的數字格式(我不知道如何)替換新的數字格式,並將其寫入新文件。
String INPUT = "D:\\input.txt";
String OUTPUT = "D:\\output.txt";
String all = "";
Pattern regexp = Pattern.compile("\"(\\d{14}.{2}\\d{4})\"");
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(INPUT), "UTF-8"))) {
String CurrentLine;
while ((CurrentLine = in.readLine()) != null) {
Matcher matcher = regexp.matcher(CurrentLine);
if (matcher.find()) {
String number1 = CurrentLine.substring(10,20);
String number2 = CurrentLine.substring(30,40);
number1 = convertor(number1);
number2 = convertor(number2);
}
all = all + sCurrentLine + "\r\n"; \\create output to use for BufferedWriter
}
順便說一句,上面的代碼(很明顯)只是我整個代碼的一部分。
要保存輸出,只需在循環結束時執行此操作。
Files.write(Paths.get(OUTPUT), all.getBytes(), StandardOpenOption.CREATE);
matcher
對象甚至返回組(\\d{14}.{2}\\d{4})
匹配的位置。 我建議仔細檢查這部分代碼以幫助自己。
使用matcher.start(1)
獲取在上一個匹配操作期間由組捕獲的子序列的起始索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.