這裏是我的文件內容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}]
請正確格式化示例代碼並使用有意義的變量名稱。事實上,這段代碼太難閱讀了。 –
有兩個布爾值,指示如果找到第一個字符串,並找到第二個字符串'if firsStringFound && secondStringFound && dateString不等於secondString然後break' –