0

我有一個文件similaire這個開始一個文件的讀取: ...從特定線

The hotspot server JVM has specific code-path optimizations 
# which yield an approximate 10% gain over the client version. 
export CATALINA_OPTS="$CATALINA_OPTS -server" 
#############HDK1001############# 

# Disable remote (distributed) garbage collection by Java clients 
# and remove ability for applications to call explicit GC collection 
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC" 

# Check for application specific parameters at startup 
if [ -r "$CATALINA_BASE/bin/appenv.sh" ]; then 
. "$CATALINA_BASE/bin/appenv.sh" 
fi 
#############HDK7564############# 
# Disable remote (distributed) garbage collection by Java clients 
# and remove ability for applications to call explicit GC collection 
export CATALINA_OPTS="$CATALINA_OPTS -XX:+DisableExplicitGC" 

我想從那裏存在單詞「HDK1001」和結束行開始讀它在世界上「HDK7564」

我tryed與此代碼,但我無法做到的限制

public static HashMap<String, String> getEnvVariables(String scriptFile,String config) { 
    HashMap<String, String> vars = new HashMap<String, String>(); 
    try { 

     FileInputStream fstream = new FileInputStream(scriptFile); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
     String strLine; 
     String var= "HDK1001"; 

     while ((strLine = br.readLine()) != null ) { 

      if (strLine.startsWith("export") && !strLine.contains("$")) { 
       strLine = strLine.substring(7); 
       Scanner scanner = new Scanner(strLine); 
       scanner.useDelimiter("="); 
       if (scanner.hasNext()) { 
        String name = scanner.next(); 
        String value = scanner.next(); 
        System.out.println(name+"="+value); 
        vars.put(name, value); 
       } 
      } 

請幫助我

+2

請不要使用DataInputStream來讀取文本。這個不好的例子被一次又一次地複製,我不相信開發者仍然這樣做。 –

+0

@Peter Lawrey以及我正在尋找替代方案 – user2592651

+0

另一種方法是刪除該行並根本不使用它。它完全是多餘的,最糟糕的是令人困惑。使用FileReader更好,但基本上與我更改代碼的內容相同。 –

回答

0

試試這段代碼。

public static HashMap<String, String> getEnvVariables(String scriptFile, 
      String config) { 
     HashMap<String, String> vars = new HashMap<String, String>(); 
     BufferedReader br = null; 
     try { 
      FileInputStream fstream = new FileInputStream(scriptFile); 
      br = new BufferedReader(new InputStreamReader(fstream)); 
      String strLine = null; 
      String stopvar = "HDK7564"; 
      String startvar = "HDK1001"; 
      String keyword = "export"; 
      do { 
       if (strLine != null && strLine.contains(startvar)) { 
        if (strLine.contains(stopvar)) { 
         return vars; 
        } 
        while (strLine != null && !strLine.contains(stopvar)) { 
         strLine = br.readLine(); 
         if (strLine.startsWith(keyword)) { 
          strLine = strLine.substring(keyword.length()) 
            .trim(); 
          String[] split = strLine.split("="); 
          String name = split[0]; 
          String value = split[1]; 
          System.out.println(name + "=" + value); 
          vars.put(name, value); 
         } 
        } 
       } 
      } while ((strLine = br.readLine()) != null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return vars; 
    } 
+0

thx這就是我需要:) – user2592651

+0

沒問題,很高興幫助 –

0

您的示例代碼很遙遠,我不打算重寫所有的代碼,但我會給你一些指示。你已經這樣做:

if (strLine.startsWith("export") && !strLine.contains("$")) 

這是你的條件應該針對「HDK1001」字符串,而不是不管它在做什麼,現在來測試。我不確定你爲什麼要檢查單詞「導出」,看起來對你的程序無關緊要。

沒有辦法只是神奇地開始和結束在文件中的特定詞語,你必須從頭開始,逐行檢查所有這些,直到你找到你想要的第一行和最後一行。一旦你找到第一行,你可以繼續閱讀,直到你達到你想要的結局,然後紓困。

+0

我想從文本中提取一部分,然後從該部分中提取每一行,以導出兩個字段開頭:名稱和值 – user2592651

0

這是僞代碼,它遵循您希望能夠完成此任務的邏輯類型。

flag = false 
inside a loop 
{ 
read in a line 
if(line != #############HDK1001############# && flag == false){ //check to see if you found your starting place 
    nothing useful here. lets go around the loop and try again 

    else // if i found it, set a flag to true 
    flag = true; 

    if(flag == true) // am i after my starting place but before my end place? 
    { 
    if(line == #############HDK1001#############) 
     do nothing and go back through the loop, this line is not useful to us 

    else if(line == #############HDK7564#############) //did i find my end place? 
     flag = false // yes i did, so lets not be able to assign stuff any more 

    else // im not at the start, im not at the end. I must be inbetwee. lets read the data and assign it. 
     read in the lines and assign it to variables that you want 
} 
+0

我試過這個,但它會寫所有行! '的BufferedReader讀者=新的BufferedReader(新的FileReader( \t \t 「E:\\ file.txt的」)); String line = reader.readLine(); (line!= null){ \t if(line!=「HDK」&& flag == false){ \t line = reader.readLine(); \t} \t else \t { \t flag = true; String word = line; System.out.println(word); line = reader.readLine(); }' – user2592651

+0

@ user2592651如果您的標誌啓動爲true,則會根據該代碼寫入所有行。也在if語句中,你不想再讀一行。當它在循環中進行時,您將讀取下一行,並且您將跳過每一行。 – user2464620