Reading and Writing File Properties in Java Swing Application

I have an assignment to create a Java Swing application to work with a mysql database, I was planning to set the database connection properties in the .properties file. In this application, the user should be able to change the database properties through the application. The problem I have is how to read and write properties file through swing app.

try {
            Properties prop = new Properties();
//reading properties
            FileInputStream in = new FileInputStream("conf/properties.xml");

//Writing properties
FileOutputStream out = new FileOutputStream("conf/properties.xml");
prop.setProperty("username", "root");
prop.storeToXML(out, "rhym");

        } catch (Exception e) {


xml file.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
    <comment>database configuration</comment>
    <entry key="driver">com.mysql.jdbc.Driver</entry>
    <entry key="ip"></entry>
    <entry key="port">3306</entry>
    <entry key="database">ofm_mnu_jvs</entry>
    <entry key="username">user1</entry>
    <entry key="password">123789</entry>



source to share

3 answers

Sounds like a programming exercise to me :)

First, you need to write code that can handle persistent Java object Properties

to disk and retrieve Properties

from disk. You can do this in many different ways, but the best way is to use the Java Properties syntax to save the object's contents Properties

to a user-editable text file. Your parser just needs to be smart enough to figure out how to read text from a file back into an object Properties

, but it really isn't that hard to do.

Once your program can correctly read / write Java file syntax from files, you can write your user interface for object instances only Properties

. The UI can tell your save objects / methods to save the instance Properties

every time the user changes a field or value.

The bottom line is the most important thing to figure out how to break this program into smaller pieces. You could just as easily write a bunch of monolithic code that directly saves your property files from ActionListeners in Swing, but none of that code will be reused. Split your code into smaller objects (Parser object, UI object), then focus only on smaller pieces, one at a time, until you can make them all work together to achieve your goal.



You are not reading / writing Properties

through a Swing app. You just read / write Properties

as you would in any Java application (and which is documented in the javadoc Properties


Once you have read the .properties file on an object Properties

, you can create a UI to customize that instance Properties

. For example, when the entry is a boolean value, you can use a checkbox in the UI and update the instance Properties

when the user toggles the selected checkbox state.


source or google Load properties file in java.

The actual reading of the properties file will be done, this is your method. Thus, its by no means dependent on Swing. Just read / load the property data into any of your collections and then execute any Swing or Swing methods to get and display it.



All Articles