2017-05-26 37 views
0

這裏是我的文件內容2017-05-05 08:24:46 to 2017-05-05 08:24:55,現在我需要打印從 - 2017-05-01 -05 08:24:49到-2017-05-05 08:24:52之間的內容。你能建議除此之外的其他方法嗎?如何在文件中獲取特定字符串

1.FILE內容:

[2017-05-05 08:24:46]contents is [ [pool-80 
thread.PaseInterceptorChain... zhjkhzjh]nkshxkjghsjkgjskgxsjgxjsagxhujgsxhjsxghjvasxhasvxhjvsaxhvshjxvhjvhxjvhsvxhj 
[2017-05-05 08:24:49]contents is [ [pool-80-thread-1] xhggajgzjh 
icationExceptionOutInterceptor... nvsjhkslkxlxskl]jsahxjahx 
[2017-05-05 08:24:49]contents is [ [pool-78-thread-1.. ] 
[2017-05-05 08:24:52]contents is [ [pool-78-thread-1... ] 
[2017-05-05 08:24:52]contents is [ [pool-78-thread-1... {... = 
contents inside}] 
[2017-05-05 08:24:55]contents is [ [pool-78-thread-1... {... = 
contents inside}] 

2,本是我的代碼:

public class MeteringlogBean implements Serializable 
{ 
private static final long serialVersionUID = 1L; 
private Date date10; 
private Date date11; 
private String d; 
private String d1; 
private String s; 
private String text; 
private FileWriter fw; 
private BufferedWriter bw; 
private String Fn = "C:/Users/swetha.papireddy/Documents/new/filename.txt"; 
public Date getDate11() { 
return date11; 
} 
public void setDate11(Date date11) { 
this.date11 = date11; 
} 
private String environment; 
public String getEnvironment() { 
return environment; 
} 
public void setEnvironment(String environment) { 
this.environment = environment; 
} 
public Date getDate10() { 
return date10; 
} 
public void setDate10(Date date10) { 
this.date10 = date10; 
} 

public void save() throws IOException, NullPointerException { 
try { 
    File file = new File(getEnvironment() + "/ischange.log"); 
    FileReader fileReader = new FileReader(file); 
    BufferedReader bufferedReader = new BufferedReader(fileReader); 
    StringBuilder stringBuilder = new StringBuilder(); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
     stringBuilder.append(line); 
     stringBuilder.append("\n"); 
    } 
    fileReader.close(); 
    System.out.println("Contents of file:"); 
    s = stringBuilder.toString(); 
    System.out.println(s); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date10); 
System.out.println("date:-" + d); 
d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date11); 
System.out.println("date:-" + d1); 

fw = new FileWriter(Fn); 
bw = new BufferedWriter(fw); 
bw.write(s); 
bw.close(); 

File file = new File(Fn); 
FileInputStream fis = new FileInputStream(file); 
byte[] bytes = new byte[(int) file.length()]; 
fis.read(bytes); 
fis.close(); 

String text = new String(bytes, "UTF-8"); 
String str1 = new String(d); 
String str2 = new String(d1); 
System.out.println(text.substring(text.indexOf(str1), 
     text.lastIndexOf(str2))); 
} 

3.my實際輸出爲

[2017-05-05 08:24:49]contents is [ [pool-80-thread-1] xhggajgzjh 
icationExceptionOutInterceptor... nvsjhkslkxlxskl]jsahxjahx 
[2017-05-05 08:24:49]contents is [ [pool-78-thread-1.. ] 
[2017-05-05 08:24:52]contents is [ [pool-78-thread-1... ] 

現在我需要得到兩[2017-05-05 08:24:52]

4.outpu T應當是:

[2017-05-05 08:24:49]contents is [ [pool-80-thread-1] xhggajgzjh 
icationExceptionOutInterceptor... nvsjhkslkxlxskl]jsahxjahx 
[2017-05-05 08:24:49]contents is [ [pool-78-thread-1.. ] 
[2017-05-05 08:24:52]contents is [ [pool-78-thread-1... ] 
[2017-05-05 08:24:52]contents is [ [pool-78-thread-1... {... = 
contents inside}] 
+0

請正確格式化示例代碼並使用有意義的變量名稱。事實上,這段代碼太難閱讀了。 –

+0

有兩個布爾值,指示如果找到第一個字符串,並找到第二個字符串'if firsStringFound && secondStringFound && dateString不等於secondString然後break' –

回答

2

您可以使用String.contains(搜索字符串)或String.endsWith()或String.startsWith() 或使用邏輯類似

while((currentLine = scanner.readLine()) != null) 
{ 
    if(currentLine.indexOf("Your String")) 
    { 
     //Do task 
     passedLine = true; 
    } 
    if(passedLine) 
    { 
     //Do other task after passing the line. 
    } 
    lineNumber++; 
} 
+0

在哪裏使用它? – Swetha

0

嘗試添加邏輯來獲取下一個日期時間值,並使用它獲取前一個值。

例如: 目前您正在獲取日誌,直到[2017-05-05 08:24:52]。在指定此String的最後一個索引之前,請嘗試在「[2017-05-05 08:24:52]」之後標識下一個即時日期時間值。 使用String.contains來獲取它。

登錄來獲取最後一行:

Current datetime = "2017-05-05 08:24:52"; 
boolean nextValueFound; 
while(!nextValueFound){ 
Date newDate = calender.add(Date, 1); // this will add seconds (not exactly. please check) 
//convert the date to String 
dateStringtobeCheckedinText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date11); 
if(text.contains(dateStringtobeCheckedinText)){ 
    nextValueFound = true; 
} 
} 

// your required text: 
System.out.println(text.substring(text.indexOf(str1), 
     text.indexOf(dateStringtobeCheckedinText))); 
+0

但在這裏我沒有給出具體的當前日期時間,我可以選擇任何日期。 date10是從日期和date11是迄今 – Swetha

+0

可以請你解釋更多..我沒有得到你的要點 – Swetha

+0

你需要搜索date10開始到結束日期11行。由於您試圖獲取的是子字符串,因此您需要在第一次出現的date10 ---第一次出現date11旁邊的日期值的情況下進行搜索,該日期值出現在您搜索的字符串中。因此,在應用子字符串之前,獲取日誌中存在的下一個日期值(StringText) –

0

閱讀文件和結果存儲在字符串生成器。

public static String getInputString(String filepath) throws IOException { 
     RandomAccessFile randomAccessFile = new RandomAccessFile 
       (filepath, "r"); 
     FileChannel channel = randomAccessFile.getChannel(); 
     ByteBuffer buffer = ByteBuffer.allocate(1024);  //Reading the file in chunk of 1024 bytes[1KB] 
     StringBuilder sb = new StringBuilder(""); 
     while(channel.read(buffer) > 0) 
     { 
      buffer.flip();         //Ready for get ,put operation 
      for (int i = 0; i < buffer.limit(); i++) 
      { 
       sb.append((char)buffer.get());    //Reading every character and appending to Stringbuilder    
      } 
      buffer.clear(); 
     }   
     channel.close(); 
     randomAccessFile.close(); 
     return sb.toString(); 
    } 

使用正則表達式來查找日期之間的日誌文本::

private HashMap<String, List<String>> parsedMap = new HashMap<String, List<String>>(); 
public HashMap<String, List<String>> getValues(String inputSTring) { 
     String regex = "(?=((("Start Date")(.*?|\n.*?)("End Date"))))"; 

     Pattern pattern = Pattern.compile(regex); 
     Matcher matcher = pattern.matcher(inputSTring); 

     while(matcher.find()) { 
      String key = matcher.group(3); 
      String value = matcher.group(4);    

      System.out.println(key + " => " + value); 
      List<String>contentsList = parsedMap.get(key); 
      if(contentsList == null) { 
       contentsList = new ArrayList<String>(); 
       parsedMap.put(key, contentsList); 
      } 
      contentsList.add(value); 
     } 
     return parsedMap;  
    } 

然後取從HashmMap的價值,創造新的輸出字符串。

+0

它不適合我..你可以給其他方法 – Swetha

+0

你需要相應地修改你的代碼。 – Vaibs

+0

可以說你在哪裏正確使用它? – Swetha

相關問題