Editing CSV Files (Design Implementation)

后端 未结 1 904

Im starting to design a program that will automate the process of finding and identifying strings correctly based on similar strings and their identities that have been found an

相关标签:
1条回答
  • 2021-01-26 14:26

    First off break the problem up into its components as you are overcomplicating it.

    The root of the problem is that you have a file with records that you are writing a gui for to allow the user to edit.

    In an effort to increase performance you want to read and write to the same file, attempting to only read or write a single record.

    The file in question is in a csv format.

    So the first one you have down cold so there is no need to go over that.

    The second part I would say do not do with many exclamation points. The reason for that is the worst case scenario - you program crashes. At which point you have corrupted your original. If you know the number of records are small then read the whole thing into memory (say as an list of strings) and parse the individual strings into their records and when the user is done and they go to save you write it into a different file that once done you delete the original and rename the second file to the first. This way if you hit the worst case scenario you either have the original file intact or the changes are there just under a different name.

    If there is too much to fit in memory at one time there is the RandomAccessFile that allows reads and writes to the same file. But I would recommend you make a copy of the file at the start (using the .tmp or .swp that some editors use) and work with that as it still protects you from the dreaded crash.

    After that it is how you deal with CSV data. If it is simple text you can use the Java String split method. If it is more complex then openCSV has the CSVParser method that will parse the String into an array of strings for you. There is also a CSVParserBuilder that simplifies constructing the parser.

    Hope that helps.

    :)

    0 讨论(0)
提交回复
热议问题