Hi,

I would like to ask regarding File Handling in Java. I am creating a program for my school project and we should use file handling only (.txt) not database. When I add a product (with product code, product name, price, etc), I want to save it in a file. After that there is an option for displaying the product. I would like to display the product in a columnar format. Can you advice me on how I can do it? or once the program reads the file, is there a way to separate the identifiers so I can format each identifiers before displaying in the system. Thank you experts

My advice is to forgo the old data in a file for this app. Use a database.

commented: Hi sir rproffitt, we are required to do a file handling.. can you help advice me on this? Thanks +0

So it's homework/assignment. That said the class should have given you the skills to do this in prior classroom instruction and assignments.

But hey, let's discuss how we store data in a file. Remember there is NO STANDARD here or far too many so I take it you're going to do this in the way/method that you would never do in apps later.

Here goes.

You collect data from the user and write it to a file. Done!

Editing said data?
Read all the data from the file into your arrays and such.
Ask the user for the changes.
Change that array entry.
Write all that data out to that file (overwrites old file.)

Simple but never something you should do IRL except for small use cases.

commented: Thank you for your answer, rproffitt. Will look at this. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.