2013-01-02 33 views
0

我正在使用java將腳本寫入帶有註釋的文本文件。在評論裏面,寫了我的sql腳本。我正在寫評論actions startsactions ends並且我希望此評論中的腳本稍後會被其他腳本替換。 我怎樣才能刪除腳本它可指定註釋內和添加新的腳本如何刪除指定的開始和結束行中的文本在java中

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

你有一個'String'或在一個文件的完整劇本? – aioobe

+0

意圖是什麼?如果你只描述一些你想要達成的目標,也許有人可以幫你找到捷徑。 – Boon

+0

我正在將所有這些腳本寫入文件。 – Muthu

回答

0

我找到了解決方案。解決方案是使用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(sectionStarts); int cnt2 = sb.indexOf(sectionEnds); 如果(!CNT1 = -1 || CNT2 = -1){ sb.replace(CNT1 + sectionStarts.length(),CNT2,LINE_BREAK +的queryString + LINE_BREAK); }

int cnt1 = sb.indexOf(sectionStarts);它標識了起始標籤和int cnt2 = sb.indexOf(sectionEnds);標識結束標籤。

它會找到塊,將更換新的內容。

+0

我想你可以將文件加載到String obj(如果文件不是很大),並使用正則表達式處理。您可能需要多線模式。我想這種方式會容易得多。 – Kent

相關問題