简体   繁体   English

Java文件访问 - 性能注意事项

[英]Java File access - Performance considerations

All, 所有,

Considering my earlier query Stackoverflow-Post , here is what I am currently doing. 考虑到我之前的查询Stackoverflow-Post ,这是我目前正在做的事情。 This is a kind of flowchart: 这是一种流程图:

1 > Add record to file : 1> 将记录添加到文件
a. 一种。 Get the details for new record 获取新记录的详细信息
b. Retrieve all the records from the file. 从文件中检索所有记录。
c. C。 Compare each record retrieved with the new record details. 将检索到的每条记录与新记录详细信息进 If match found, do not add to the file, else add the new record. 如果找到匹配项,请不要添加到文件中,否则添加新记录。

2 > Search for a record in file . 2> 在文件中搜索记录
a. 一种。 Get the details for the record to be searched (Here only the credit card number.) 获取要搜索的记录的详细信息(此处仅显示信用卡号。)
b. Retrieve all the records from the file. 从文件中检索所有记录。
c. C。 Compare each record retrieved with the record searched for. 将检索到的每条记录与搜索到的记录进行比较 If match found, display the details. 如果找到匹配项,则显示详细信息。

3 > Delete a record from the file . 3> 从文件中删除记录
a. 一种。 Get the details for the record to be deleted (Here only the credit card number.) 获取要删除的记录的详细信息(此处仅显示信用卡号。)
b. Retrieve all the records from the file. 从文件中检索所有记录。
c. C。 Compare each record retrieved with the record searched for. 将检索到的每条记录与搜索到的记录进行比较 If match found, delete the entry from the file. 如果找到匹配项,请从文件中删除该条目。

As can be seen, I am doing an IO every time for a operation. 可以看出,我每次都在进行操作。 Is there any way I can optimize or improve performance for my code? 有什么方法可以优化或提高我的代码的性能吗?

The API I am using for Add record is FileWriter , for search operation it is BufferedReader and for deleting a record is BufferedReader and PrintWriter 我用于添加记录的API是FileWriter ,对于搜索操作,它是BufferedReader ,用于删除记录的是BufferedReaderPrintWriter

Use a database. 使用数据库。

Use an index file for the credit card number. 使用索引文件作为信用卡号。 The index file may be structured like a hash or a tree and contains a pointer to the offset of your data file. 索引文件可以像散列或树一样构造,并包含指向数据文件偏移量的指针。 The lookup in the index file is fast, since you have a single key (credit card number). 索引文件中的查找速度很快,因为您只有一个密钥(信用卡号)。

Calculate hashes for your credit card numbers and store them in the index. 计算您的信用卡号码的哈希值并将其存储在索引中。 That way, you can quickly identify whether an entry exists or not. 这样,您就可以快速识别条目是否存在。

I would also suggest you use a database but if you must use a file then an option i can suggest is Java serialization .Maintain a HashMap with Key as the credit-card number and the rest of the information wrapped in an object as the value.Each time a new records are added add the information to the hashmap and serialize the data and delete the old file.Same you can do for deletion of records and for searching its very easy since the information is in a hashmap , all you have to do is check if the key is present or not and another advantage is you don't have to bother about the format for writing into a file and then parsing it again. 我还建议您使用数据库,但如果您必须使用文件,那么我可以建议的选项是Java序列化。使用Key作为信用卡号,并将包含在对象中的其余信息作为值,保留HashMap。每次添加新记录时都会将信息添加到hashmap并序列化数据并删除旧文件。由于信息在hashmap中,所以你可以做的就是删除记录并进行搜索非常容易。检查密钥是否存在,另一个好处是您不必费心写入文件然后再次解析它的格式。

If the number of records are large a problem that can probably occur is that you have to load the large file into memory.A solution is you can maintain a limit for the number of records per file and you can maintain a look-up with a navigable map( check this link for example ).A range of credit card number's can be stored in a certain file.The key for the look-up can be the starting range of the credit-card and the the value can the file name. 如果记录数量很大,则可能出现的问题是您必须将大文件加载到内存中。解决方案是您可以维护每个文件的记录数限制,并且可以使用可导航地图( 例如,查看此链接 )。信用卡号码的范围可以存储在某个文件中。查找的键可以是信用卡的起始范围,值可以是文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM