简体   繁体   English

在Java Swing应用程序中读取和写入属性文件

[英]Reading and writing properties file in a java Swing application

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. 我有一个创建java Swing应用程序的任务,用mysql数据库做一些事情,我已经计划在.properties文件中设置数据库连接属性。 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. 我遇到的问题是如何通过swing应用程序读取和写入属性文件。

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 file.. 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 :) 对我来说听起来像程序设计练习:)

First, you need to write code that can handle persisting Java's Properties object to disk, and retrieving Properties from disk. 首先,您需要编写可以将持久化Java的Properties对象处理到磁盘并从磁盘检索Properties 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. 您可以通过多种方式执行此操作,但最好的方法是使用Java Properties语法将Properties对象的内容保存到用户可编辑的文本文件中。 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. 你的解析器必须足够聪明才能弄清楚如何将文件从文件读回到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. 一旦您的程序能够从文件中正确读取/写入Java属性语法,您就可以编写用户界面以仅处理Properties对象实例。 The UI could tell your persistence objects/methods to save the Properties instance each time the user changes a field or value. 每次用户更改字段或值时,UI都可以告诉您的持久性对象/方法保存Properties实例。

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. 您可以轻松编写一堆直接从Swing中的ActionListeners保存属性文件的单片代码,但这些代码都不可重用。 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. 将代码分解为较小的对象(Parser对象,UI对象),然后只关注那些较小的部分,一次一个,直到您可以让它们一起工作以实现您的目标。

You do not read/write the Properties through a Swing application. 您不通过Swing应用程序读取/写入Properties You just read/write the Properties as you would do it in any Java application (and which is documented in the class javadoc of the Properties class) 您只需在任何Java应用程序中读取/写入Properties (并在Properties类的类javadoc中记录)

Once you read the .properties file into a Properties object, you can build a UI to customize that Properties instance. 将.properties文件读入Properties对象后,可以构建UI以自定义该Properties实例。 For example when an entry represents a boolean value, you can use a checkbox in the UI, and update the Properties instance when the user toggles the selected state of the checkbox. 例如,当条目表示布尔值时,您可以使用UI中的复选框,并在用户切换复选框的选定状态时更新“ Properties实例。

http://www.java-tips.org/java-se-tips/java.util/how-to-read-and-write-a-properties-file.html or google Load properties file in java . http://www.java-tips.org/java-se-tips/java.util/how-to-read-and-write-a-properties-file.html或google在java中加载属性文件。

Actually reading a properties file will be done is some of your method. 实际上读取属性文件将是你的一些方法。 So its not at all depends upon Swing. 所以它根本不取决于Swing。 Just read/load the property data in any of your collections and then execute any Swing methods or Swing components to fetch the same and display. 只需在任何集合中读取/加载属性数据,然后执行任何Swing方法或Swing组件以获取它们并显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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