Java 如何在lastindexof中获取相同匹配的多个字符串? 如何在lastindexof中获取相同匹配的多个字符串?
在这里,我想得到两个字符串,在lastindexof中,从08:24:49到08:24:52,但是它只打印一个08:24:52,但是我想得到两个08:24:52 2017-05-05 08:24:49408警告Java 如何在lastindexof中获取相同匹配的多个字符串? 如何在lastindexof中获取相同匹配的多个字符串?,java,Java,在这里,我想得到两个字符串,在lastindexof中,从08:24:49到08:24:52,但是它只打印一个08:24:52,但是我想得到两个08:24:52 2017-05-05 08:24:49408警告 public class MeteringlogBean implements Serializable { private static final long serialVersionUID = 1L; private Date date10; private Date date1
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();
}
// System.out.println(date10);
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)));
}
2017-05-05 08:24:49408警告
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();
}
// System.out.println(date10);
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)));
}
2017-05-05 08:24:52408警告
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();
}
// System.out.println(date10);
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)));
}
String text ="2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:52,408 WARN 2017-05-05 08:24:52,408 WARN";
String searchStr = "08:24:52";
while(text.length()>= searchStr.length() && text.contains(searchStr)){
text = text.substring(text.indexOf(searchStr)+searchStr.length());
System.out.println(searchStr);
}
2017-05-05 08:24:52408警告
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();
}
// System.out.println(date10);
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)));
}
String text ="2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:52,408 WARN 2017-05-05 08:24:52,408 WARN";
String searchStr = "08:24:52";
while(text.length()>= searchStr.length() && text.contains(searchStr)){
text = text.substring(text.indexOf(searchStr)+searchStr.length());
System.out.println(searchStr);
}
输出:
2017-05-05 08:24:49408警告
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();
}
// System.out.println(date10);
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)));
}
2017-05-05 08:24:49408警告
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();
}
// System.out.println(date10);
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)));
}
2017-05-05 08:24:52408警告
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();
}
// System.out.println(date10);
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)));
}
String text ="2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:49,408 WARN 2017-05-05 08:24:52,408 WARN 2017-05-05 08:24:52,408 WARN";
String searchStr = "08:24:52";
while(text.length()>= searchStr.length() && text.contains(searchStr)){
text = text.substring(text.indexOf(searchStr)+searchStr.length());
System.out.println(searchStr);
}
希望这能回答您的问题。我真的不明白您想要做什么,但我认为您不能使用通用字符串类。 为什么不使用Pattern和Match类来找出这种情况下的字符串呢 您提供的代码没有为我完成,因此,我对其进行了修改,添加了一小段代码 然后,如果我有下面这样的日志文件格式 ischange.log文件 我生成的最终输出文件没有空字符串,名为ischange.log.out,如
2017-05-05 08:24:49,408 WARN
2017-05-05 08:24:49,408 WARN
2017-05-05 08:24:52,408 WARN
2017-05-05 08:24:52,408 WARN
完整的源代码在这里
private String getEnvironment() {
return ".";
}
public void save() throws IOException, NullPointerException {
String s = null;
String d = null, d1 = null;
Date date10 = new Date();
Date date11 = new Date();
BufferedWriter bw = null;
String Fn = getEnvironment() + "/ischange.log.out";
BufferedReader bufferedReader = null;
TreeMap<String, String> hashStore = new TreeMap<String, String>();
FileInputStream fis = null;
try {
File file = new File(getEnvironment() + "/ischange.log");
if (!file.exists())
return;
bufferedReader = new BufferedReader(new FileReader(file));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals(""))
continue;
stringBuffer.append(line);
stringBuffer.append("\n");
StringTokenizer st = new StringTokenizer(line, ",");
if (st.countTokens() != 2)
continue;
hashStore.put(st.nextToken(), st.nextToken());
}
System.out.println("Contents of file:");
s = stringBuffer.toString();
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
} finally {
bufferedReader.close();
}
Set<String> eSet = hashStore.keySet();
String[] outDate = new String[2];
outDate = eSet.toArray(outDate);
try {
date10 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(outDate[0]);
date11 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(outDate[1]);
} catch (ParseException e) {
e.printStackTrace();
}
String formatStr = "yyyy-MM-dd HH:mm:ss";
formatStr = "HH:mm:ss";
d = new SimpleDateFormat(formatStr).format(date10);
System.out.println("date:- " + d);
d1 = new SimpleDateFormat(formatStr).format(date11);
System.out.println("date:- " + d1);
try
{
bw = new BufferedWriter(new FileWriter(Fn));
bw.write(s);
}
finally
{
bw.close();
}
byte[] contents = null;
try
{
int len = s.getBytes().length;
fis = new FileInputStream(new File(Fn));
contents = new byte[len];
fis.read(contents, 0, len);
}
finally
{
fis.close();
}
String text = new String(contents, "UTF-8");
Pattern pattern = Pattern.compile(d);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(" match : " + matcher.group());
}
pattern = Pattern.compile(d1);
matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(" match : " + matcher.group());
}
// String text = new String(bytes, "UTF-8");
//String str1 = new String(d);
//String str2 = new String(d1);
//int index = text.indexOf(str1);
//int indexlast = text.lastIndexOf(str2);
//System.out.println("index:- " + index + " indexlast: " + indexlast);
// System.out.println(text.substring(index,indexlast));
//System.out.println(text.substring(text.indexOf(str1), text.lastIndexOf(str2)));
}
public static void main(String[] args) {
try {
new StringTest().save();
} catch (NullPointerException | IOException e) {
e.printStackTrace();
}
}
使用TreeMap逐行读取字符串,同时对其进行排序
TreeMap<String, String> hashStore = new TreeMap<String, String>();
...
while ((line = bufferedReader.readLine()) != null) {
if (line.equals(""))
continue;
stringBuffer.append(line);
stringBuffer.append("\n");
StringTokenizer st = new StringTokenizer(line, ",");
if (st.countTokens() != 2)
continue;
hashStore.put(st.nextToken(), st.nextToken());
}
如果您有以下文件
2017-05-05 08:24:46,397 WARN [pool-80-thread.PhaseInterceptorChain...
2017-05-05 08:24:49,444 ERROR [pool-80-thread-1] icationExceptionOutInterceptor...
2017-05-05 08:24:49,408 WARN [pool-78-thread-1..
2017-05-05 08:24:52,408 ERROR [pool-78-thread-1...
2017-05-05 08:24:52,408 ERROR [pool-78-thread-1... {... = contents inside}
我的实施将有两个条件。
1.日期字符串的长度是固定的,这是19字节的长度
2.日志级字符串也是固定的,它们是警告、调试或错误
这是我的主意
public void save2() throws IOException, NullPointerException {
final String LOG_LVL[] = {"WARN", "DEBUG", "ERROR"};
final class LogData
{
private final String dateString;
private final String contents;
private final int logtypelen;
LogData(final String dateString, final String contents, final int logtypelen)
{
this.dateString = dateString;
this.contents = contents;
this.logtypelen = logtypelen;
}
String getDateString()
{
return dateString.substring(0,19);
}
String getContents()
{
return contents.substring(logtypelen,contents.length());
}
}
BufferedReader bufferedReader = null;
List<LogData> strList = new ArrayList<LogData>();
try {
File file = new File(getEnvironment() + "/ischange2.log");
if (!file.exists())
return;
bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals(""))
continue;
int i = 0;
int idx = -1;
for(i = 0; i < LOG_LVL.length; i++)
if((idx = line.indexOf(LOG_LVL[i])) != -1) break;
StringTokenizer st = new StringTokenizer(line, ",");
if (st.countTokens() != 2)
continue;
strList.add(new LogData(st.nextToken(), line, idx+LOG_LVL[i].length()));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
bufferedReader.close();
}
for(LogData data : strList)
{
System.out.println("date is [" + data.getDateString() + "]===== contents is [" + data.getContents() + "]");
}
}
如何填写date10和date11?提示:您从未在方法的签名中写下“抛出Nullpointerexception”。没有意义,表明您不理解该异常的含义。无关:不要使用
StringBuffer
,而是使用StringBuilder
。为什么要用默认字符集写入文件,然后用UTF-8读回?有没有理由试图以这种方式破坏文本?为什么要这样做?你记忆中有文本,所以就用它另外,如果d
已经是一个String
,那么新字符串(d)
有什么意义呢?这是我的ischange文件:2017-05-05 08:24:46397 WARN[pool-80-thread.PhaseInterceptorChain…2017-05-05 08:24:49444 ERROR[pool-80-thread-1]异常异常执行器…2017-05-05-08:24:49408 WARN[pool-78-thread-1..2017-05-05 08:24:52408错误[pool-78-thread-1…2017-05-05 08:24:52408错误[pool-78-thread-1…{…=内部内容]。在这里,我需要读取日期之间的内容,而不仅仅是匹配的关键字。您是否有一个文件,每行都有回车符和换行符,或者一个文件中只有连续的字符块?实际上没有。只有一块数据,因为我在上面提供了一个示例。如果是这样,我有自己的想法来编码您的日志文件。请参阅我编辑的解决方案。