Java教程

【计理01组30号】Java 实现日记写作软件

本文主要是介绍【计理01组30号】Java 实现日记写作软件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目分析

代码设计

com.shiyanlou.entity

User.java

package com.shiyanlou.entity;

public class User {

    private String ID;  
    private String name; 
    private String passwd;


    public String getName() {
        return name;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getID() {
        return ID;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

}

com.shiyanlou.util

Diary.java

package com.shiyanlou.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.text.Document;

public class Diary {

    public static void addDiary(String pathname, String title, String txt) {
        File dirfile = new File(pathname);
        BufferedWriter bufw = null;
        dirfile.mkdirs();

        File file = new File(dirfile, title + ".ky");
        try {
            bufw = new BufferedWriter(new FileWriter(file, true));
            bufw.write(txt);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            if (bufw != null) {
                try {
                    bufw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public static void read(File file, Document doc) {

        try (BufferedReader bufr = new BufferedReader(new FileReader(file));) {
            String txt = null;
            String line = System.getProperty("line.separator");
            while ((txt = bufr.readLine()) != null) {

                doc.insertString(doc.getLength(), txt + line, null);

            }

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

}

JDOM.java

package com.shiyanlou.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

public class JDOM {
    public static String write(String n, String p, String id) {
        // TODO Auto-generated method stub
        String path = "/home/shiyanlou/Desktop/UserInfo.xml";
        File file = new File(path);
        SAXBuilder saxBuilder = new SAXBuilder();
        Document doc; 
        try {
            doc = saxBuilder.build(file);
            Element root = doc.getRootElement(); //\u5F97\u5230\u6839\u5143\u7D20
            Element user = new Element("User"); //\u5EFA\u7ACBUser\u5143\u7D20
            Element name = new Element("name");//\u5EFA\u7ACBname\u5143\u7D20
            Element passwd = new Element("passwd");//\u5EFA\u7ACBpasswd\u5143\u7D20
            if (checkID(id, root)) {
                user.setAttribute(new Attribute("id", id));
                name.setText(n);
                passwd.setText(p);
                user.addContent(name);
                user.addContent(passwd);
                root.addContent(user);
                XMLOutputter out = new XMLOutputter();
                out.output(doc, new FileOutputStream(file));
                return "Successful registration";//\u8FD4\u56DE\u6CE8\u518C\u6210\u529F
            } else
                return "ID already exists, please input again";

        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "ERROR";

    }

    public static boolean checkID(String id, Element root) {
        boolean flag = true;
        @SuppressWarnings("unchecked")
        List<Element> list = root.getChildren("User");
        Iterator<Element> it = list.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            if (e.getAttributeValue("id").equals(id)) {
                flag = false;
            }
        }
        return flag;

    }

    public static String read(String id, String passwd) {
        String path = "/home/shiyanlou/Desktop/UserInfo.xml";
        File file = new File(path);
        SAXBuilder saxBuilder = new SAXBuilder();

        try {
            Document doc = saxBuilder.build(file);
            Element root = doc.getRootElement();

            String info = getPasswd(root).get(id);
            if (info == null) {
                return "User does not exist!!";
            }
            String[] buf = info.split("/");

            if (buf[0].equals(passwd)) {
                return "Successful landing/" + buf[1];
            }

        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "Wrong password!!";
    }

    @SuppressWarnings("unchecked")
private static Map<String, String> getPasswd(Element root) {
        Map<String, String> map = new TreeMap<String, String>();//\u5B58\u8D2E\u7528\u6237\u4FE1\u606F
        List<Element> list = new ArrayList<Element>();
        list = root.getChildren("User");
        Iterator<Element> it = list.iterator();
        while (it.hasNext()) {
            Element e = it.next();
            String id = e.getAttributeValue("id");
            String passwd = e.getChildText("passwd");
            String name = e.getChildText("name");
            map.put(id, getInfo(passwd, id));
        }

        return map;

    }

    private static String getInfo(String passwd, String name) {

        return passwd + "/" + name;

    }
}

Register.java

package com.shiyanlou.util;

import com.shiyanlou.entity.User;

public class Register {

    static User user = new User();

    public static String checkName(String name) {
        user.setName(name);
        return null;

    }

    public static String checkID(String ID) {
        if (ID.matches("\\d{1,8}")) {
            user.setID(ID);
            return null;
        } else
            return "ID not conform to the rules";
    }

    public static String checkPasswd(String passwd) {
        if (passwd.matches("\\d{6,15}")) {
            user.setPasswd(passwd);
            return null;
        } else
            return "Password not conform to the rules";
    }

    public static String register(String name,String passwd,String ID) {
        user.setName(name);
        user.setPasswd(passwd);
        user.setID(ID);
        return (JDOM.write(user.getName(), user.getPasswd(),
                user.getID()));

    }
}

com.shiyanlou.view

IndexGUl.java

package com.shiyanlou.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class IndexGUI extends JFrame {
    
    private JPanel contentPane;
    private static IndexGUI frame;

    public static void main(String[] args) {
        init();
    }
    public static void init() 
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new IndexGUI(); 
                    frame.setVisible(true); 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public IndexGUI() {
        setTitle("KnowYou");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setBounds(100, 100, 650, 400);
        contentPane = new JPanel(); 
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
        setContentPane(contentPane); 
        contentPane.setLayout(null);  

        JLabel lblNewLabel = new JLabel("Welcome to use KnowYou"); 
        lblNewLabel.setBounds(132, 74, 386, 35);
        lblNewLabel.setFont(new Font("Ubuntu", Font.BOLD | Font.ITALIC, 30));
        contentPane.add(lblNewLabel);

        JButton login = new JButton("Login"); 
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                event_Login(); 
            }
        });


        login.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    event_Login();
                }
            }
        });
        login.setBounds(65, 263, 124, 45);
        contentPane.add(login);

        JButton register = new JButton("Sign Up"); 

        register.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                event_register(); 
            }
        });

        register.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    event_register();
                }
            }
        });
        register.setBounds(489, 263, 109, 45);
        contentPane.add(register);

    }

    private void event_Login()
    {
        setVisible(false);
        new LoginGUI().loginGUI();
    }

    private void event_register()
    {
        setVisible(false);
        new RegisterGUI().registerGUI();
    }
}

LoginGUl.java

package com.shiyanlou.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.shiyanlou.util.JDOM;


public class LoginGUI extends JFrame {
    private static final long serialVersionUID = 4994949944841194839L;
    private JPanel contentPane;  
    private JTextField IDtxt; 
    private JLabel Passwdlabel;
    private JPasswordField passwordField;
    private JButton login;
    private JButton back;

    /**
     * Launch the application.
     * @return
     */
    public void loginGUI() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginGUI frame = new LoginGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 650, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel IDlabel = new JLabel("Please input ID");
        IDlabel.setBounds(68, 170, 100, 39);
        contentPane.add(IDlabel);

        IDtxt = new JTextField();
        IDtxt.setBounds(220, 179, 126, 21);
        contentPane.add(IDtxt);
        IDtxt.setColumns(10);

        Passwdlabel = new JLabel("Please input password");
        Passwdlabel.setBounds(68, 219, 150, 50);
        contentPane.add(Passwdlabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(220, 234, 126, 21);
        contentPane.add(passwordField);

        login = new JButton("login");
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                event_login();
            }
        });
        login.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                {
                    event_login();
                }
            }
        });
        login.setBounds(239, 310, 93, 23);
        contentPane.add(login);
        back = new JButton("BACK");
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                 IndexGUI.init();
                 setVisible(false);
            }
        });
        back.setBounds(507, 310, 93, 23);
        contentPane.add(back);
        JLabel label = new JLabel("Welcome to use KnowYou");
        label.setFont(new Font("Ubuntu", Font.BOLD | Font.ITALIC, 30));
        label.setBounds(142, 54, 386, 35);
        contentPane.add(label);
    }

    private void event_login()
    {
        String id=IDtxt.getText();
        String passwd=new String(passwordField.getPassword());
        String flag=JDOM.read(id, passwd);
        if(flag.contains("Successful landing"))
        {
            String[] bufs=flag.split("/");
            String name=bufs[1];
            JOptionPane.showMessageDialog(contentPane, "Welcome: "+name,"Welcome",JOptionPane.PLAIN_MESSAGE);
            UsersGUI.init(name);
            setVisible(false);
        }
       else
       {
     JOptionPane.showMessageDialog(contentPane,flag,"ERROR",JOptionPane.ERROR_MESSAGE);
       }
     }
}

RegisterGUl.java

package com.shiyanlou.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.shiyanlou.util.Register;

public class RegisterGUI extends JFrame {

    private static final long serialVersionUID = 3250371445038102261L;
    private JPanel contentPane;
    private JTextField nametext;  
    private JTextField IDtext;  
    private JTextField passwdtext;  

    /**
     * Launch the application.
     */
    public void registerGUI() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RegisterGUI frame = new RegisterGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public RegisterGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 650, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel namelabel = new JLabel("Please input user name"); 
        namelabel.setBounds(102, 91, 151, 23);
        contentPane.add(namelabel);
        JLabel IDlabel = new JLabel("Please input user ID");
        IDlabel.setBounds(102, 160, 151, 23);
        contentPane.add(IDlabel);
        JLabel passwdlaber = new JLabel("Please input user password");
        passwdlaber.setBounds(102, 224, 163, 23);
        contentPane.add(passwdlaber);
        nametext = new JTextField(); 
        nametext.setBounds(271, 92, 92, 21); 
        contentPane.add(nametext);
        nametext.setColumns(10);  
        IDtext = new JTextField();
        IDtext.setBounds(271, 161, 92, 21);
        contentPane.add(IDtext);
        IDtext.setColumns(8);
        passwdtext = new JTextField();
        passwdtext.setBounds(271, 225, 92, 21);
        contentPane.add(passwdtext);
        passwdtext.setColumns(10);
        JButton register = new JButton("Sign Up");
        register.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                register.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        String name = nametext.getText();
                        String ID = IDtext.getText();
                        String passwd = passwdtext.getText();
                       if (Register.checkID(ID) == null) {
                        if (Register.checkPasswd(passwd) == null) {
                        String srt = Register.register(name, passwd, ID);
                        JOptionPane.showMessageDialog(contentPane,srt,"information", JOptionPane.PLAIN_MESSAGE);
                        setVisible(false);
                        new IndexGUI().init();
                        } else {
                        JOptionPane.showMessageDialog(contentPane,Register.checkPasswd(passwd), "ERROR", JOptionPane.ERROR_MESSAGE);
                        }
                        } else {
                        JOptionPane.showMessageDialog(contentPane,Register.checkID(ID), "ERROR",
                 JOptionPane.ERROR_MESSAGE);
                                }
                        }
                        });
            }
        });

        register.setBounds(321, 305, 93, 23);
        contentPane.add(register);

        JButton back = new JButton("BACK");  //\u8FD4\u56DE\u6309\u94AE
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                IndexGUI.init(); 
                setVisible(false); 
            }
        });
        back.setBounds(531, 305, 93, 23);
        contentPane.add(back);

        JLabel label = new JLabel("Welcome to use KnowYou"); 
        label.setFont(new Font("Ubuntu", Font.BOLD | Font.ITALIC, 30));
        label.setBounds(143, 26, 374, 35);
        contentPane.add(label);

        JLabel lblNewLabel = new JLabel("(There are 1 to 8 numbers)");
        lblNewLabel.setBounds(373, 164, 163, 15);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("(There are 6 to 15 numbers)");
        lblNewLabel_1.setBounds(373, 228, 163, 15);
        contentPane.add(lblNewLabel_1);
    }
}

UsersGUl.java

package com.shiyanlou.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.shiyanlou.util.Diary;



public class UsersGUI extends JFrame {
    private JPanel contentPane;
    private JTextField textField;

    private JFileChooser chooser;

    private static String pathname;

    public static void init(String path) { //\u521D\u59CB\u5316\u65B9\u6CD5
        pathname = path;
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UsersGUI frame = new UsersGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UsersGUI() {
    	
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setToolTipText("KonwYou");
        tabbedPane.setBounds(0, 0, 574, 67);
        contentPane.add(tabbedPane);

        final JPanel panel = new JPanel();
        tabbedPane.addTab("Management Journal", null, panel, null);

        chooser = new JFileChooser(".\\"+pathname);
        FileNameExtensionFilter filter=new FileNameExtensionFilter("Allowed","ky");
        chooser.setFileFilter(filter);

        JButton readButton = new JButton("Read the diary");
        
        readButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                int value = chooser.showOpenDialog(panel);

                JInternalFrame internalFrame_Read = new JInternalFrame("Read the diary", false, true, false, false);
                internalFrame_Read.setBounds(0, 77, 584, 275);
                contentPane.add(internalFrame_Read);
                internalFrame_Read.getContentPane().setLayout(null);
                JTextPane txtDiary = new JTextPane();
                txtDiary.setBounds(0, 0, 568, 246);
                internalFrame_Read.getContentPane().add(txtDiary);

                javax.swing.text.Document doc=txtDiary.getDocument();
                txtDiary.setBackground(Color.GREEN);
                txtDiary.setEditable(false);
                
                if (value == JFileChooser.APPROVE_OPTION) {

                       File file = chooser.getSelectedFile();
                  
                    if(file.exists()) {

                    	Diary.read(file, doc);

                        internalFrame_Read.setVisible(true);
                    }
                }
            }
        });

        panel.add(readButton);

        JButton addButton = new JButton("Create a diary");//\u65B0\u5EFA\u6309\u94AE
        addButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                final JInternalFrame internalFrame_Write = new JInternalFrame("Create a diary",false, true, false, false);
                

                internalFrame_Write.setBounds(0, 77, 584, 275);
                contentPane.add(internalFrame_Write);
                   internalFrame_Write.getContentPane().setLayout(null);

                textField = new JTextField();
                textField.setBounds(76, 0, 492, 21);
                internalFrame_Write.getContentPane().add(textField);
                textField.setColumns(10);

                JLabel label = new JLabel("Title");

                label.setFont(new Font("\u6977\u4F53", Font.PLAIN, 12));
                   label.setBounds(46, 3, 52, 15);
                   internalFrame_Write.getContentPane().add(label);

                final JEditorPane editorPane = new JEditorPane();
                editorPane.setBounds(0, 31, 568, 179);
                internalFrame_Write.getContentPane().add(editorPane);

                JButton save = new JButton("SAVE");
                save.setBounds(465, 213, 93, 23);
                save.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        String title = textField.getText();
                        String txt = editorPane.getText();
                        Diary.addDiary(pathname, title, txt);
                        internalFrame_Write.setVisible(false);
                    }
                });
                internalFrame_Write.getContentPane().add(save);
                internalFrame_Write.setVisible(true);
            }
        });

        panel.add(addButton);
        JButton delButton = new JButton("Delete");
        delButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                File file=null;
                int value=chooser.showOpenDialog(panel);
                if(value==JFileChooser.APPROVE_OPTION)
                {
                    file=chooser.getSelectedFile();
                    int x=JOptionPane.showConfirmDialog(panel,"Confirm delete?","Please confirm",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

                    if(file.exists())
                    {
                        if(x==JOptionPane.OK_OPTION) {
                            file.delete();
                            JOptionPane.showMessageDialog(panel, "Delete Success!","information", JOptionPane.PLAIN_MESSAGE);
                        }
                    }

                }

            }
        });

        panel.add(delButton);
        JButton back = new JButton("BACK");
        back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                IndexGUI.init();
                setVisible(false);
            }
        });

        panel.add(back);
    }
}

要点解析

XML文档

需要对应着 User 类来设计一个 XML 文档,用于持久化存储用户信息。

什么是 XML

  • XML 指可扩展标记语言(EXtensible Markup Language)
  • XML 是一种标记语言,很类似 HTML
  • XML 的设计宗旨是传输数据,而非显示数据
  • XML 标签没有被预定义。您需要自行定义标签。
  • XML 被设计为具有自我描述性。
  • XML 是 W3C 的推荐标准

创建 XML 文件

在桌面空白处右键点击,选择从模版创建一个空文件。

文件名可以填写为 UserInfo.xml。

创建完成后可以在桌面上找到这个 UserInfo.xml 文件。

右键点击该文件,用 gedit 编辑器打开它。

编辑 XML 文件

在编辑器中输入 XML 文件的信息和项目中会用到的标签 Users。

<?xml version="1.0" encoding="UTF-8"?> <Users></Users>

编辑完成后,点击保存按钮保存该文件,然后关闭编辑器。 UserInfo.xml 如图:

以上就完成了我们保存用户信息的 xml 文档。

JDOM

JDOM 是一种使用 XML(标准通用标记语言下的一个子集) 的独特 Java 工具包。它的设计包含 Java 语言的语法乃至语义。

JDOM 的用法

要使用 JDOM 解析 XML 文件,需要下载 JDOM 的包,实验中使用的是 jdom-1.1。解压之后,将 lib 文件夹下的 *.jar 文件以及 build 文件夹下的 jdom.jar 拷贝到工程文件夹下,然后就可以使用 JDOM 操作 XML 文件了。

在实验环境中下载 jdom 可以使用下面的方式,打开 Xfce 终端,输入命令:

$ wget https://labfile.oss.aliyuncs.com/courses/480/jdom-2.0.6.zip $ unzip jdom-2.0.6.zip

JDOM 的具体实现

首先需要将 JDOM 的 jar 包导入到我们的工程中。

右键点击项目目录,选择 Properties 进入项目属性设置。

在 Java Build Path 设置项里切换到 Libraries 选项卡,然后点击右侧的 Add External JARs... 按钮。

在弹出的 JAR Selection 对话框中选择 Shiyanlou 目录下刚解压的 JDOM 相关包 jdom-2.0.6.jar,然后点击 确定 按钮完成添加。

最后在属性页点击 OK 完成设置。

接下来就需要对 JDOM 类进行编辑。

在类 JDOM.java中,主要包含了两个方法,write() 和 read() 方法,分别用于将用户信息写入到 xml 文档中和读出用户信息。

这篇关于【计理01组30号】Java 实现日记写作软件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!