hier in meinem Dateiinhalt 2017-05-05 08:24:46 bis 2017-05-05 08:24:55, jetzt muss ich von 2017-05 drucken -05 08:24:49 bis -2017-05-05 08:24:52 Inhalt zwischen dem Datum. Können Sie eine andere Methode als diese vorschlagen?Wie bekomme ich eine bestimmte Zeichenfolge in einer Datei
1.file Inhalt:
[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.this ist mein Code:
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 tatsächliche Ausgabe ist:
[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... ]
jetzt ich zwei bekommen müssen [2017-05-05 08:24:52]
4.outpu t sollte sein:
[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}]
Formatieren Sie den Beispielcode richtig und verwenden Sie sinnvolle Variablennamen. Wie es ist, ist dieser Code zu schwer zu lesen. –
Haben Sie zwei Booleans, die anzeigen, wenn die erste gefundene Zeichenfolge und die zweite gefundene Zeichenfolge 'wenn firsStringFound && secondStringFound && dateString nicht gleich secondString ist, dann break' –