简体   繁体   中英

How I can read some specific data from a file that contain different types of data?

I need to write some data about several companies into a binary file; the data includes name of companies (String), year of incorporation (int), and revenue this year (double). Write data would be not trouble to me, but I don't know how to read some specific data from a the binary file; for example, if I have name of a company, I need to return the the other data about the company. So far, I only know to read all the data or maybe line by line in a binary file. Can someone suggest which class or method I need to use to do so?

You should read about serialization/deserialization of objects in Java .
Serialize list of companies (eg ArrayList<Company> companies ) then simply deserialize it, so you can use the same list of companies again in your code then search through list to find the company you want. Optionally, you can use Map<String, Company> as JB Nizet suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM