[英]How to remove the text within the specified starting and ending lines in java
I am writing scripts to a text file with comments using java. 我正在使用Java将脚本编写到带有注释的文本文件中。 Inside the comment, my sql script is written. 在注释中,编写了我的sql脚本。 I am writing my scripts within the comment actions starts
and actions ends
and I want the script within this comment to be replaced by some other scripts later. 我在注释actions starts
和actions ends
内编写脚本,我希望以后将此注释内的脚本替换为其他脚本。 How can I remove the script which is available inside the specified comment and add new script . 如何删除指定注释中可用的脚本并添加新脚本 。
--
-- `actions` starts
--
CREATE TABLE IF NOT EXISTS `actions` (
`aid` varchar(255) NOT NULL DEFAULT '0' COMMENT 'Primary Key: Unique actions ID.',
`type` varchar(32) NOT NULL DEFAULT '' COMMENT 'The object acts on ',
PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores action.';
--
-- `actions` ends
--
--
-- `operations` starts
--
CREATE TABLE IF NOT EXISTS `operations` (
`type` varchar(32) NOT NULL DEFAULT '' COMMENT 'The object that that action acts on ',
`callback` varchar(255) NOT NULL DEFAULT '' COMMENT 'The callback ',
`parameters` longblob NOT NULL COMMENT 'Parameters to be passed to .',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores operations.';
--
-- `operations` ends
--
I found the solution. 我找到了解决方案。 Solutions is to use indexOf method to identify the starting and ending blocks. 解决的办法是使用indexOf方法来标识开始和结束块。 The code that i used is, 我使用的代码是
private static final String VARIABLE_FIELD = "variable";
private static final String END_MODULE_END_TAG = "' ends";
private static final String START_MODULE_END_TAG = "' starts";
private static final String MODULE_START_TAG = "-- '";
private static final String DOUBLE_HYPHEN = "--";
private static final String VALUE_FIELD = "value";
private static final String NAME_FIELD = "name";
private static final String SQL_VARIABLE_SEP = "`,`";
private static final String SQL_VALUE_SEP = "','";
private static final String SINGLE_QUOTE = "'";
private static final String LINE_BREAK = "\n";
private static final String EQUAL = "=";
File scriptFile = new File(versionFile + File.separator + fileName);
StringBuffer sb = new StringBuffer();
if (scriptFile.isFile()) {
// if script file is available need to replace the content
buff = new BufferedReader(new FileReader(scriptFile));
String readBuff = buff.readLine();
String sectionStarts = MODULE_START_TAG + moduleName + START_MODULE_END_TAG;
String sectionEnds = MODULE_START_TAG + moduleName + END_MODULE_END_TAG;
while (readBuff != null) {
sb.append(readBuff);
sb.append(LINE_BREAK);
readBuff = buff.readLine();
}
int cnt1 = sb.indexOf(sectionStarts);
int cnt2 = sb.indexOf(sectionEnds);
if (cnt1 != -1 || cnt2 != -1) {
sb.replace(cnt1 + sectionStarts.length(), cnt2, LINE_BREAK + queryString + LINE_BREAK);
} else {
// if this module is not added already in the file and need to add this config alone
sb.append(LINE_BREAK + DOUBLE_HYPHEN + LINE_BREAK);
sb.append(MODULE_START_TAG + moduleName + START_MODULE_END_TAG + LINE_BREAK);
sb.append(queryString);
sb.append(LINE_BREAK);
sb.append(MODULE_START_TAG + moduleName + END_MODULE_END_TAG + LINE_BREAK);
sb.append(DOUBLE_HYPHEN + LINE_BREAK);
}
}
The key code is 关键代码是
int cnt1 = sb.indexOf(sectionStarts); int cnt1 = sb.indexOf(sectionStarts); int cnt2 = sb.indexOf(sectionEnds); int cnt2 = sb.indexOf(sectionEnds); if (cnt1 != -1 || cnt2 != -1) { sb.replace(cnt1 + sectionStarts.length(), cnt2, LINE_BREAK + queryString + LINE_BREAK); 如果(cnt1!= -1 || cnt2!= -1){sb.replace(cnt1 + sectionStarts.length(),cnt2,LINE_BREAK + queryString + LINE_BREAK); } }
int cnt1 = sb.indexOf(sectionStarts); int cnt1 = sb.indexOf(sectionStarts); it identifies the starting tag and int cnt2 = sb.indexOf(sectionEnds); 它标识开始标记,并且int cnt2 = sb.indexOf(sectionEnds); identifies the ending tag. 标识结束标记。
It will find the block and will replace the content with new one. 它将找到该块,并将内容替换为新的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.