What is the best way to edit the middle of an existing flat file?

家住魔仙堡 提交于 2019-12-11 04:41:18

问题


I have tool that creates variables for a simulation. The current workflow involves hand copying those variables into the simulation input file. The input file is a standard flat file, i.e. not binary or XML. I would like to automate the addition of the variables to the flat input file.

The variables copy over existing variables in the file, e.g.

New Variables: Length 10 Height 20 Depth 30

Old Variables: ... Weight 100 Age 20 Length 10 Height 20 Depth 30 ...

Would like to have the old variables copy over the new variable. They are 200 lines into the flat input file.

Thanks for any insights.

P.S. This is on Windows.


回答1:


If you're stuck using flat, then you're stuck using the old fashioned way of updating them: read from original, write to temp file, either write the original row or change the data and then write that. To add data, write it to the temp file at the appropriate point; to delete data, simply don't copy it from the original file.

Finally, close both files and rename the temp file to the original file name.

Alternatively, it might be time to think about a little database.




回答2:


For something like this I'd be looking at a simple template engine. You'd have a base template with predefined marker tokens instead of variable values and then just pass the values required to your engine along with the template and it will spit out the resultant file, all present and correct. There are a number of Open Source template engines available in Java that would meet your needs, I imagine such things are also available in your language of choice. You could even roll your own without too much difficulty.



来源:https://stackoverflow.com/questions/732469/what-is-the-best-way-to-edit-the-middle-of-an-existing-flat-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!