如何在java中删除指定起始行和结束行内的文本

如何在java中删除指定起始行和结束行内的文本,java,string,text,replace,Java,String,Text,Replace,我正在使用java将脚本写入带有注释的文本文件。在注释中,编写了我的sql脚本。我正在注释操作开始和操作结束中编写脚本,我希望稍后将此注释中的脚本替换为其他脚本如何删除指定注释中可用的脚本,以及如何添加新脚本 -- -- `actions` starts -- CREATE TABLE IF NOT EXISTS `actions` ( `aid` varchar(255) NOT NULL DEFAULT '0' COMMENT 'Primary Key: Unique actions I

我正在使用java将脚本写入带有注释的文本文件。在注释中,编写了我的sql脚本。我正在注释
操作开始
操作结束
中编写脚本,我希望稍后将此注释中的脚本替换为其他脚本如何删除指定注释中可用的脚本,以及如何添加新脚本

--
-- `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
--

我找到了解决办法。解决方案是使用indexOf方法来识别起始块和结束块。我使用的代码是

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);
    }
}
关键代码是

int cnt1=sb.indexOf(分段开始); int cnt2=sb.indexOf(分段结束); 如果(cnt1!=-1 | | cnt2!=-1){ sb.替换(cnt1+sectionStarts.length(),cnt2,换行符+查询字符串+换行符); }

int cnt1=sb.indexOf(分段开始)它标识起始标记和int cnt2=sb.indexOf(sectionEnds)标识结束标记


它将找到块并用新的块替换内容。

您将完整脚本保存在
字符串或文件中?目的是什么?如果你再多描述一下你想要实现的目标,也许有人能帮你找到一条捷径。我正在将所有这些脚本都写在一个文件中。你想在生成文件时(使用Java、从另一个文件或其他源)或在运行文件时(在MySQL中)进行替换吗?既然它被标记为
java
,那么我假设它是前者是正确的吗?(下一个问题是-如何生成它,最好包括一些代码)旁注-根据我的理解,行话是脚本是一组SQL语句(或代码)(在文件中是SQL脚本),您希望用其他语句替换两个注释之间的一些语句。在任何地方使用
script
都是非常混乱的。我认为您可以将文件加载到字符串obj中(如果文件不是很大的话),然后使用regex进行处理。您可能需要多行模式。我想用这种方法会容易得多。