[英]How to replace specific line only on 1st occurence by mentioning keyword
我有以下 pom.xml 文件。 我想替換第 7 行中的項目版本。我將能夠單獨提供<version>
標記,但不能提供完整的版本詳細信息。 期望找到第一個<version>
標記行並將其替換為<version>1.0.1</version>
的代碼
我從下面的 java 代碼開始,但目前正在替換整個 pom.xml 中的整個版本標記。
如何用<version>1.0.1</version>
單獨替換第一個<version>
標記行
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspace</groupId>
<artifactId>Test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>kjar</packaging>
<name>Test</name>
<description></description>
<dependencies>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>7.46.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>7.46.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-core</artifactId>
<version>7.46.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-persistence-jaxb</artifactId>
<version>7.46.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.kie</groupId>
<artifactId>kie-maven-plugin</artifactId>
<version>7.46.0.Final</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Java 代碼
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OverwriteLine {
public static void main(String args[]) throws IOException {
//Instantiating the File class
String cloneDirectoryPath = "D:\\project\\localrepo117";
String filePath = cloneDirectoryPath+"\\pom.xml";
//Instantiating the Scanner class to read the file
Scanner sc = new Scanner(new File(filePath));
//instantiating the StringBuffer class
StringBuffer buffer = new StringBuffer();
//Reading lines of the file and appending them to StringBuffer
while (sc.hasNextLine()) {
buffer.append(sc.nextLine()+System.lineSeparator());
}
String fileContents = buffer.toString();
System.out.println("Contents of the file: "+fileContents);
//closing the Scanner object
sc.close();
String oldLine = "<version>";
String newLine = "<version>1.0.1</version>";
//Replacing the old line with new line
fileContents = fileContents.replaceAll(oldLine, newLine);
//instantiating the FileWriter class
FileWriter writer = new FileWriter(filePath);
System.out.println("");
System.out.println("new data: "+fileContents);
writer.append(fileContents);
writer.flush();
}
}
我建議您逐行閱讀文件“pom.xml”。 如果讀取的行是您要更改的行,則更改它,否則保持原樣。 然后將您讀取的行寫入臨時文件。 閱讀完文件“pom.xml”中的所有行后,將其刪除並將臨時文件重命名為“pom.xml”。
下面的代碼就是這樣做的。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class OverwriteLine {
public static void main(String[] args) {
Path sourcePath = Path.of("pom.xml");
Path targetPath = Path.of("tmp.xml");
boolean error = false;
try (BufferedReader br = Files.newBufferedReader(sourcePath);
BufferedWriter bw = Files.newBufferedWriter(targetPath,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE)) {
boolean found = false;
String line = br.readLine();
while (line != null) {
if (!found) {
if (line.trim().startsWith("<version>")) {
found = true;
line = line.replace("1.0.0", "1.0.1");
}
}
bw.write(line);
bw.write(System.lineSeparator());
line = br.readLine();
}
}
catch (IOException xIo) {
error = true;
xIo.printStackTrace();
}
if (!error) {
try {
Files.delete(sourcePath);
Files.move(targetPath, sourcePath);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.