⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyfilereader.java

📁 Usefull sample codes for Java. Containt all type of programs.
💻 JAVA
字号:
import java.io.*;
import nu.xom.*;

public class PropertyFileReader {
    String comment;
    String username;
    String browser;
    boolean showEmail;
    
    public PropertyFileReader() throws ParsingException, IOException {
        // get the XML document
        File propFile = new File("properties.xml");
        Builder builder = new Builder();
        Document doc = builder.build(propFile);
        // get the root element, <properties>
        Element root = doc.getRootElement();
        // get the <comment> element and store it in a variable
        Element commentElement = root.getFirstChildElement("comment");
        comment = commentElement.getValue();
        // get the <entry> elements
        Elements entries = root.getChildElements("entry");
        for (int current = 0; current < entries.size(); current++) {
            // get current <entry>
            Element entry = entries.get(current);
            // get entry's key attribute
            Attribute key = entry.getAttribute("key");
            String keyValue = key.getValue();
            // store attribute value in the proper variable
            if (keyValue.equals("username")) {
                username = entry.getValue();
            }
            if (keyValue.equals("browser")) {
                browser = entry.getValue();
            }
            if (keyValue.equals("showEmail")) {
                String emailValue = entry.getValue();
                if (emailValue.equals("yes")) {
                    showEmail = true;
                } else {
                    showEmail = false;
                }
            }
        }
    }
    
    public void showProperties() {
        System.out.println("\nProperties\n");
        System.out.println("Comment: " + comment);
        System.out.println("Username: " + username);
        System.out.println("Browser: " + browser);
        System.out.println("Show Email: " + showEmail);
    }
    
    public static void main(String[] arguments) {
        try {
            PropertyFileReader reader = new PropertyFileReader();
            reader.showProperties();
        } catch (Exception exception) {
            System.out.println("Error: " + exception.getMessage());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -