英文:
How to search lines contain string in file.txt
问题
I have a problem when I want to search for a string line in a file. This is my code:
public static void main(String[] args) throws SQLException {
try {
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);
}
}
And here's my method for searching a string in a file:
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));
}
And when I use:
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);
I got:
[183394, 183395, 183396, 183397, 183398, 183399, 183400, 183401, 183402, 183403, 183404, 183405, 183406]
When I run my code, I got an error; it shows "false." But when I search manually, I find the values in the file:
name of the file is 2020-05-05-EURUSD.txt
EUR/USD|1.09031|2020-05-05T04:30:17.008
EUR/USD|1.0903|2020-05-05T04:30:17.046
...
EUR/USD|1.09027|2020-05-05T04:30:17.848
How can I fix the issue with searching for the string in the file.txt
?
And here is the log that I got:
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
英文:
i have problem when i want to search string line in file this is my code
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);
}
}
and here my method for search String in file
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));
}
and when i use
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);
}
i got [183394, 183395, 183396, 183397, 183398, 183399, 183400, 183401, 183402, 183403, 183404, 183405, 183406]
when i run my code i got error there is false.
but when i search manual there the value is
name of file is 2020-05-05-EURUSD.txt
EUR/USD|1.09031|2020-05-05T04:30:17.008
EUR/USD|1.0903|2020-05-05T04:30:17.046
EUR/USD|1.09029|2020-05-05T04:30:17.211
EUR/USD|1.0903|2020-05-05T04:30:17.340
EUR/USD|1.09027|2020-05-05T04:30:17.348
EUR/USD|1.0903|2020-05-05T04:30:17.509
EUR/USD|1.09027|2020-05-05T04:30:17.518
EUR/USD|1.0903|2020-05-05T04:30:17.547
EUR/USD|1.09027|2020-05-05T04:30:17.558
EUR/USD|1.0903|2020-05-05T04:30:17.712
EUR/USD|1.09027|2020-05-05T04:30:17.718
EUR/USD|1.0903|2020-05-05T04:30:17.841
EUR/USD|1.09027|2020-05-05T04:30:17.848
how to fix search string in the file.txt ?
and this is the log what i got
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
答案1
得分: 0
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);
}
英文:
fileContents.contains(timeSearch)
checks if any element of fileContents (each line of your file) are equal to timeSearch
you should use String contains()
method for each element of fileContents (each line of your file)
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);
}
答案2
得分: 0
private static void SearchFile(String timeSearch, String filesearch) throws IOException {
Scanner fileScanner = new Scanner(new File(filesearch));
int lineID = 0;
java.util.List<Integer> lineNumbers = new ArrayList<>();
ArrayList<Double> list = new ArrayList<Double>();
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);
String lines = Files.readAllLines(Paths.get(filesearch)).get(lineID);
String[] LineGetPrice = lines.split("\\|", 2);
String price = LineGetPrice[1];
String[] pricestringArray = price.split("\\|", 2);
String prices = pricestringArray[0];
Double priced = Double.parseDouble(prices);
list.add(priced);
//Double MinPrice = getMinValue(list);
System.out.println(lines + prices);
//System.out.println(list);
}
}
System.out.println(lineNumbers);
System.out.println(list);
Double MinPrice = getMinValue(list);
System.out.println(list + "|" + MinPrice);
}
英文:
private static void SearchFile(String timeSearch, String filesearch) throws IOException {
Scanner fileScanner = new Scanner(new File(filesearch));
int lineID = 0;
java.util.List lineNumbers = new ArrayList();
ArrayList<Double> list = new ArrayList<Double>();
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);
String lines = Files.readAllLines(Paths.get(filesearch)).get(lineID);
String[] LineGetPrice = lines.split("\\|", 2);
String price = LineGetPrice[1];
String[] pricestringArray = price.split("\\|", 2);
String prices=pricestringArray[0];
Double priced = Double.parseDouble(prices);
list.add(priced);
//Double MinPrice = getMinValue(list);
System.out.println(lines+prices);
//System.out.println(list);
}
}
System.out.println(lineNumbers);
System.out.println(list);
Double MinPrice = getMinValue(list);
System.out.println(list+"|"+MinPrice);
}
i have update and use this code i can get price and all line for match the data
thanks for all who help me here
专注分享java语言的经验与见解,让所有开发者获益!
评论