Java在文件中的特定文本上方插入带有字符串缩进的文本行

huangapple go评论57阅读模式
英文:

Java insert text line above specifice text in string from file with string indentation

问题

我从文件中读取简单的文本,并将其内容作为字符串获取

String pomxml = "c:\\foo\\test.json";
String content = FileUtils.readFileToString(file);

字符串内容(示例)为:

"email_settings": {
    "email.starttls.enable":"true",
    "email.port":"111",
    "email.host":"xxxx",
    "email.auth":"true",
}

我想在"email.host":"xxxx"上方插入新的字符串,仅当找到它时。

所以它将看起来像:

"email_settings": {
    "email.starttls.enable":"true",
    "email.port":"111",
    "email.name":"myTest",
    "email.host":"xxxx",
    "email.auth":"true",
}

我的问题是如何将这行新内容插入字符串中

更新
在此示例中,它是JSON,但也可以是普通文本或XML文件
因此我不能依赖于JSON提供程序

英文:

i have simple text im reading from file and I'm getting its content as string

String pomxml = "c:\\foo\\test.json";
String content = FileUtils.readFileToString(file); 

the string content (example) is :

"email_settings": {
	  "email.starttls.enable":"true",
	  "email.port":"111",
	  "email.host":"xxxx",
	  "email.auth":"true",
}

i like to insert new string above "email.host":"xxxx", only if it finds it.
so it will look like :

"email_settings": {
	  "email.starttls.enable":"true",
	  "email.port":"111",
	  "email.name":"myTest",
	  "email.host":"xxxx",
	  "email.auth":"true",
}

My question is how to insert this new line into the string

UPDATE
in this example it is JSON , but it can be also simple text or XML file
so i can't rely on JSON providers

答案1

得分: 2

你可以尝试在这里进行正则表达式替换:

String input = " \"email_settings\": {\n \"email.starttls.enable\":\"true\",\n \"email.port\":\"111\",\n \"email.host\":\"xxxx\",\n \"email.auth\":\"true\",\n}";
String output = input.replaceAll("([ ]*\"email.host\":\".*?\")", "      \"email.name\":\"myTest\",\n$1");
System.out.println(output);

这将打印出:

"email_settings": {
      "email.starttls.enable":"true",
      "email.port":"111",
      "email.name":"myTest",
      "email.host":"xxxx",
      "email.auth":"true",
}

然而,如果你在处理正确的JSON内容,那么你应该考虑使用JSON解析器。将JSON文本解析为Java POJO,然后加入新字段后再进行写出。

英文:

You could try doing a regex replacement here:

String input = "\"email_settings\": {\n      \"email.starttls.enable\":\"true\",\n      \"email.port\":\"111\",\n      \"email.host\":\"xxxx\",\n      \"email.auth\":\"true\",\n}";
String output = input.replaceAll("([ ]*\"email.host\":\".*?\")", "      \"email.name\":\"myTest\",\n$1");
System.out.println(output);

This prints:

"email_settings": {
	  "email.starttls.enable":"true",
	  "email.port":"111",
	  "email.name":"myTest",
	  "email.host":"xxxx",
	  "email.auth":"true",
}

However, if you are dealing with proper JSON content, then you should consider using a JSON parser instead. Parse the JSON text into a Java POJO and then write out with the new field.

答案2

得分: 1

content = content.replace(""email.host":"", ""email.name":"myTest",\\n" + "      "email.host":"");

或者你可以查阅解析 JSON 文件的库。

英文:
content = content.replace("\"email.host\":", "\"email.name\":\"myTest\",\n" + "      \"email.host\":");

Or you could look in to libraries which parse json files.

huangapple
  • 本文由 发表于 2020年10月15日 15:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64366896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定