天天看點

java 将資料寫入檔案,如何通過Java将資料寫入檔案?

java 将資料寫入檔案,如何通過Java将資料寫入檔案?

I want to make a GUI application that contains three functions as follows:

Add a record

Edit a record

Delete a record

A record contains two fields - Name and Profession

There are two restrictions for the application

You can't use database to store info. You have to use a flat file.

Total file should not be re-written for every add/delete operation.

So, my questions are mentioned below:

Q1. Which file format would be better? (.xml or .csv or .txt or any other)

Q2. How can we perform the add/delete operation without the whole file being re-written?

解決方案

As for the format - I would go with something as simple as possible. You don't want to have to deal with a bunch of markup processing, as using RandomAccessFile, you will going directly to a byte position. A fixed width style format would be good, so that based on the record number, you can calculate the starting position of a record or field in the file, without having to read everything in the file. The fields would then be padded out to the fixed width with spaces or some other suitable character.