从JAVA中的文件高性能删除字符串 [英] High Performance deletion of a String from file in JAVA
问题描述
无论发生什么,订单都不能错过。因此,无论我们收到什么订单,我们都以字符串的形式存储在文件中,当用户取消订单或订单被填充时,我们从文件中删除相应的字符串。
用户也可以一次取消50多个订单。因此,从文件中删除字符串的一般方法是:将所有字符串存储在文件中并通过在列表中搜索来删除它,这会造成非常高的延迟。
对于一个取消请求,这不是一个问题,但是当用户想要一次取消所有的订单,那么这个方法会造成很大的问题。 b
$ b
我的当前代码是从文件中删除一个字符串:
public void deleteFromFile(String clordID)throws InterruptedException {
//根据文件中的clorid删除
BufferedReader in = null;
String line = null;
尝试{
in = new BufferedReader(new FileReader(dir +\\pendingOrders.txt));
列表< String> listLines = new ArrayList<>(); ((line = in.readLine())!= null){
if(!(line.contains(clordID))){//检查Order ID是唯一的为每个字符串
listLines.add(line);
}
}
in.close();
BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(dir +\\pendingOrders.txt));
bufferedWriter1.write();
bufferedWriter1.close();
BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter(dir +\\\\\\\\\\\\\\\\\,true));
for(String msg:listLines){
bufferedWriter2.write(msg +\\\
);
}
//in.close();
bufferedWriter2.close();
$ b $ catch(IOException ex){
System.err.println(ex.getMessage());
} //删除
有没有办法提高性能?每当取消请求提出时,将所有字符串存储到列表中,再次清除文件并写入文件肯定会导致高延迟。
div>尽管我提到了使用文件的缺陷,但是看看你的代码,我可以找到以下改进:
$ b $ ol
中删除该字符串,并将其放在cancelledOrders.txt或fulfilledOrders.txt中。
$ b PS:同样,正如我在评论中指出的那样,还有很多其他人,如果这是真正的电子商务网站并正在投入生产,事情将是一场灾难。
We work on ecommerce domain. We process every order at very high speed. Every order can be cancelled and replaced according to the user.
Whatever it happens the orders must not get missed. So as a result whatever order we receive, we store in a file in the form of a string and when the user cancels the order or the order gets filled then we delete the respective String from the file.
There can also be a situation like user can cancel more than 50 orders at a time. As a result the general method for deleting the String from a file : storing all the strings in a file and delete it by searching in the list is causing a very high latency.
For one cancel request it is not a problem but when the user wants to cancel all the orders at a time then this method causing a big issue.
My current code for deleting a string from the file:
public void deleteFromFile(String clordID) throws InterruptedException {
//delete based on clorid from the file
BufferedReader in = null;
String line = null;
try {
in = new BufferedReader(new FileReader(dir + "\\pendingOrders.txt"));
List<String> listLines = new ArrayList<>();
while ((line = in.readLine()) != null) {
if (!(line.contains(clordID))) { // check for Order ID which is unique for every string
listLines.add(line);
}
}
in.close();
BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(dir + "\\pendingOrders.txt"));
bufferedWriter1.write("");
bufferedWriter1.close();
BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter(dir + "\\pendingOrders.txt", true));
for (String msg : listLines) {
bufferedWriter2.write(msg + "\n");
}
//in.close();
bufferedWriter2.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}//Delete
Is there a way to improve the performance? Every time a cancel request raises, storing all the strings into the list and again clearing the file and writing into the file is definitely causing high latency.
Despite my comment where I mention the pitfalls of using files, looking at your code, I can find the following improvements,
- If clorID is unique, why are you checking it right till the end of file? Shouldn't you break out of your for loop as soon as you find the line that contains it?
- Since clorID is unique, it is going to be there only once in your file. Why do you need to create a list then?
- If there is a pattern in which clorID is created (if it is a sequence, timestamp etc.), then create your file based on the pattern. (For ex. clorid 1 - 10000 go to File1, 10001 to 20000 go to File2 and so on.) This will drastically reduce the time taken.
- Strangely, though you mention in your statement that you want to delete the string, your code doesn't do that. What you are doing is writing the same string again! Why? What you should be doing is remove the string from pendingOrders.txt and put it in either cancelledOrders.txt or fulfilledOrders.txt
PS : Again, as I have pointed out in my comment and so have many others, if this is real eCommerce site and is going into production, things would be a disaster.
这篇关于从JAVA中的文件高性能删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!