在 Java Swing 应用程序中读取和写入属性文件 [英] Reading and writing properties file in a java Swing application

查看:51
本文介绍了在 Java Swing 应用程序中读取和写入属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务要创建一个 java Swing 应用程序来使用 mysql 数据库做一些事情,我计划在 .properties 文件中设置数据库连接属性.在该应用程序中,用户应该能够通过应用程序更改数据库属性.我遇到的问题是如何通过 Swing 应用程序读取和写入属性文件.

I have an assignment to create a java Swing application to do some stuff with a mysql database, I have planed to set the database connection properties in a .properties file. In that application user should be able to change the database properties through the application. The problem I've got is how to read and write a properties file through a swing application.

try {
            Properties prop = new Properties();
//reading properties
            FileInputStream in = new FileInputStream("conf/properties.xml");
            prop.loadFromXML(in);           
            System.out.println(prop.getProperty("driver"));
            in.close();

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

        } catch (Exception e) {
            e.printStackTrace();
        }

xml 文件..

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

推荐答案

对我来说听起来像是一个程序设计练习 :)

Sounds like a program design exercise to me :)

首先,您需要编写代码来处理将 Java 的 Properties 对象持久化到磁盘,并从磁盘检索 Properties.您可以通过多种方式执行此操作,但最好的方法是使用 Java 属性语法将 Properties 对象的内容保存到用户可编辑的文本文件中.您的解析器必须足够聪明,才能弄清楚如何将文件中的文本读回到 Properties 对象中,但这实际上并不难.

First, you need to write code that can handle persisting Java's Properties object to disk, and retrieving Properties from disk. You can do this in many ways, but the best way is to use Java Properties syntax to save the contents of the Properties object to a user-editable text file. Your parser will just have to be smart enough to figure out how to read text from the file back into a Properties object, but it's really not that hard to do.

一旦您的程序能够从文件中正确读取/写入 Java 属性语法,您就可以编写用户界面来仅处理 Properties 对象实例.每次用户更改字段或值时,UI 都可以告诉您的持久性对象/方法保存 Properties 实例.

Once your program is able to correctly read/write Java Properties syntax from files, you can write your User Interface to deal only with Properties object instances. The UI could tell your persistence objects/methods to save the Properties instance each time the user changes a field or value.

最重要的是弄清楚如何将这个程序分解成更小的部分.您可以轻松地编写一堆单体代码,直接从 Swing 中的 ActionListeners 保存您的属性文件,但这些代码都不可重用.将您的代码分解为更小的对象(解析器对象、UI 对象),然后只关注那些更小的部分,一次一个,直到您可以让它们一起工作以实现您的目标.

Bottom line is, it's most important to figure out how to break up this program into smaller pieces. You could just as easily write a bunch of monolithic code that directly saves your properties files from ActionListeners in Swing, but none of that code would be reuseable. Break your code up into smaller objects (Parser object, UI object), then focus only on those smaller pieces, one at a time, until you can get them all working together to accomplish your goal.

这篇关于在 Java Swing 应用程序中读取和写入属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆