Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse file to object in java?

I have the following thing to do :
-read data from file.txt And then parse it to our object.

-For example I have the folllowing ferrari.txt

  • and inside I have Car{mark='ferrari', model='sf90', productionYear=2020, hp=1000}
    now I should parse this to the Object Car

But all over what I could find on google is not looking like what I should use
(maybe I search wrong)
Is someone here that can give me a good link that I can get the correct info?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot "parse" that format. You could do that if you serialized the Car Object in a binary file. But since you are using a plain text file, something like this would not be possible. The format is also not a well-known one (such as JSON). If it were one of the well-known formats, you could use one of the many available libraries out there to do it for you. You could try to map the fields, but that is not that straightforward.

My suggestion is to change the format to JSON. It is human understandable, quick to work with and there is a variety of libraries supporting it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading