gpt4 book ai didi

java - 如何获取文件中的特定字符串

转载 作者:行者123 更新时间:2023-12-02 02:48:37 24 4
gpt4 key购买 nike

在我的文件内容2017-05-05 08:24:46到2017-05-05 08:24:55中,现在我需要打印从-2017-05-05 08:24:49到-2017 -05-05 08:24:52 日期之间的内容。你能建议除此之外的其他方法吗?

1.文件内容:

[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.我的实际输出是:

 [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.输出应该是:

[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}]

最佳答案

您可以使用 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++;
}

关于java - 如何获取文件中的特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com