gpt4 book ai didi

java - 如何搜索file.txt中包含字符串的行

转载 作者:行者123 更新时间:2023-12-01 16:54:51 26 4
gpt4 key购买 nike

当我想在文件中搜索字符串行时遇到问题,这是我的代码

public static void main( String[] args ) throws SQLException
{
try{
//System.out.println( "Hello World!" );
RepositoryFactory.emf=Persistence.createEntityManagerFactory("onezero");
String Symbol = "EURUSD";
TradingHistory tradingHistorys = RepositoryFactory.getTradingHistoryRepo().findPriceEmpty(Symbol);
String symbols = tradingHistorys.getSymbol();
String created = tradingHistorys.getCreated_at();
String[] createds = created.split(" ");
String tanggal = createds[0];
String times = createds[1];
String timeSearch = tanggal.concat("T").concat(times);
String pathfile = "/home/ec2-user/saveFile/";
String filesearch = pathfile.concat(tanggal).concat("-").concat(symbols).concat(".txt");
SearchFile(timeSearch,filesearch);

System.out.print(symbols+"|"+created+"|"+filesearch+timeSearch);
}catch (Exception e){
System.out.print(e);
}
}

这是我在文件中搜索字符串的方法

private static void SearchFile(String timeSearch, String filesearch) {
String line = null;
ArrayList<String> fileContents = new ArrayList<String>();
try {
FileReader fReader = new FileReader(filesearch);
BufferedReader fileBuff = new BufferedReader(fReader);
while ((line = fileBuff.readLine()) != null) {
fileContents.add(line);
}
fileBuff.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(fileContents.contains(timeSearch));
}

当我使用

Scanner fileScanner = new Scanner(new File(filesearch));
int lineID = 0;
java.util.List lineNumbers = new ArrayList();
Pattern pattern = Pattern.compile(timeSearch);
Matcher matcher = null;
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
lineID++;
matcher = pattern.matcher(line);
if(matcher.find()){
lineNumbers.add(lineID);

}


}
System.out.println(lineNumbers);
}

我得到了[183394、183395、183396、183397、183398、183399、183400、183401、183402、183403、183404、183405、183406]

当我运行我的代码时,我收到错误,错误。但是当我搜索手册时,值是

文件名称为 2020-05-05-EURUSD.txt 欧元/美元|1.09031|2020-05-05T04:30:17.008 欧元/美元|1.0903|2020-05-05T04:30:17.046 欧元/美元|1.09029|2020-05-05T04:30:17.211 欧元/美元|1.0903|2020-05-05T04:30:17.340 欧元/美元|1.09027|2020-05-05T04:30:17.348 欧元/美元|1.0903|2020-05-05T04:30:17.509 欧元/美元|1.09027|2020-05-05T04:30:17.518 欧元/美元|1.0903|2020-05-05T04:30:17.547 欧元/美元|1.09027|2020-05-05T04:30:17.558 欧元/美元|1.0903|2020-05-05T04:30:17.712 欧元/美元|1.09027|2020-05-05T04:30:17.718 欧元/美元|1.0903|2020-05-05T04:30:17.841 欧元/美元|1.09027|2020-05-05T04:30:17.848

如何修复 file.txt 中的搜索字符串?

这是我得到的日志

false
EURUSD|2020-05-05 04:30:17|2020-05-05 04:30:17/home/ec2-user/saveFile/2020-05-05-EURUSD.txt2020-05-05T04:30:17

最佳答案

fileContents.contains(timeSearch)检查 fileContents 的任何元素(文件的每一行)是否等于 timeSearch

您应该对 fileContents 的每个元素(文件的每一行)使用 String contains() 方法

public static void main(String[] args) {
String filePath2 = Paths.get("").toAbsolutePath().toString();
String path = filePath2.concat("\\files\\2020-05-05-EURUSD.txt");
searchFile("2020-05-05T04:30:17", path);
}

private static void searchFile(String timeSearch, String filesearch) {
String line = null;
ArrayList<String> fileContents = new ArrayList<String>();
try {
FileReader fReader = new FileReader(filesearch);
BufferedReader fileBuff = new BufferedReader(fReader);
while ((line = fileBuff.readLine()) != null) {
fileContents.add(line);
}
fileBuff.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(fileContents.contains(timeSearch));

boolean result =false;
for (String elementOfArrayList : fileContents) {
if(elementOfArrayList.contains(timeSearch)) {
result=true;
break;
}
}
System.out.println(result);
}

关于java - 如何搜索file.txt中包含字符串的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61607239/

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