N
N
NoMoneyException2016-08-11 18:30:36
Java
NoMoneyException, 2016-08-11 18:30:36

How to properly organize file I/O in Java?

Greetings :)
I started to deal with file I / O in Java and the large presence of all sorts of classes leads to confusion, what is better to use and how to work with it. Therefore, I ask you to suggest or throw off the manual, which describes the correct version of working with files.
My files are some kind of database for educational purposes. Type name-surname-age.
It is necessary to delete, change and add lines in these files. Adding and reading was done using bufferedReader and bufferedWriter wrappers. Is this the right choice? How to properly organize access to the middle of the file? Through seek()?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2016-08-11
@r_zaycev

But wouldn't it be more convenient to use a serializer?
Keep the object in memory when you need to flush it to disk with ObjectOutputStream , read when you need with ObjectInputStream .
I'm not an expert in Java (more experience in C#), but poking around in planetext files is the last century (: Even maybe for learning purposes, it's better to use in training what you are likely to use in real practice.

S
Sanan Yuzb, 2016-08-11
@Sanan07

Use BufferedReader или BufferedWriter, and if necessary then RandomAccess

E
Evhen, 2016-08-12
@EugeneP2

If for educational purposes, then first think over your file format. For example, at the beginning of the file there is a file header: number of fields, then enumeration of fields, field name, type, size, etc. etc. After the header comes the data itself.
To work with structured files in Java, there are two stream classes DataInputStream / DataOutputStream
. They provide methods for reading / writing all primitive Java types.
Once I made a utility for uploading / downloading data from / to a SQL server from / to a dbf file. So, the library (I don’t remember) used DataInputStream / DataOutputStream to work with dbf. A couple of gigabyte files were "uploaded" to the server in seconds, so it was written to the dbf file in seconds.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question