I'm making a console-based Java/Groovy application that does a lot of find/replaces in text files. If, say, the program knows you replaced foo
with bar
last time, it should by default know you probably want to replace the next foo
with bar
as well. My idea was to pre-populate the input field of the What would you like to rename this to?
prompt so that you don't have to retype it unnecessarily, but I can't find an easy way to do this in Java.
Is t开发者_C百科his possible, and if so, would it be a recommended practice?
Why don't you just assume that an empty imput is equal to the last input inserted? It would be quite easier to manage that pre-filling a stdin
..
in any case if you plan to have a recent list I would suggest you to have a special character combination to tell last one or the previous and so on.
There are a lot of useful approaches to this problem. You can remember only the last value the user replaced, or you can mantain a cache with the last n replacements, in case the user requests a similar replacement. Or you can ignore the previous input and force the user to provide a replacement every time you need. The right approach depends on the frequency of the replacement operations, if the user replaces the same value very often, etc. You have to choose the right solution depending on your problem domain and on the kind of interaction you want to provide to the user.
Implementing these solutions is simple. The most intuitive is saving the replacement cache in a simple text file, loading it during startup and saving the cache at the shutdown. The replacement cache can also be serialized to the file, insted of being written as plain text.
精彩评论