如何通过Java将数据写入文件? [英] How to write data to a file through java?

查看:371
本文介绍了如何通过Java将数据写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要制作一个包含以下三个功能的GUI应用程序:

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

  • 添加记录
  • 编辑记录
  • 删除记录

一条记录包含两个字段-名称和行业

A record contains two fields - Name and Profession

该应用程序有两个限制

  • 您不能使用数据库来存储信息.您必须使用平面文件.
  • 不应为每个添加/删除操作重写总文件.

所以,我的问题在下面提到:

So, my questions are mentioned below:

Q1.哪种文件格式会更好? (.xml或.csv或.txt或其他任何格式)

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

Q2.如何在不重写整个文件的情况下执行添加/删除操作?

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

推荐答案

在下面回答您问题的第二部分:

The second part of your question is answered here : Best Way to Write Bytes in the Middle of a File in Java

关于格式-我会尽量简化.您不需要处理大量标记处理,因为使用RandomAccessFile,您将直接进入字节位置.固定宽度样式格式会很好,因此基于记录号,您可以计算记录或字段在文件中的开始位置,而不必读取文件中的所有内容.然后使用空格或其他合适的字符将字段填充到固定宽度.

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.

这篇关于如何通过Java将数据写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆