java程序中my.ini_帮忙写个JAVA 读写ini配置文件小程序!!!!!
展開全部
其實使用 JDK 里面提供的e68a843231313335323631343130323136353331333335306263 Properties 最方便。 相關使用方法可以自己去查看 JDK 的API文檔。 package product;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;public class IniReader {
private Properties properties = new Properties();
private String iniPath = "test/product/pro.ini"; //ini 文件的路徑
private JFrame jFrame = new JFrame("讀取配置示例");
private JLabel jLabel1 = new JLabel("用戶登錄IP");
private JTextField jTextField1 = new JTextField(30);
private JLabel jLabel2 = new JLabel("端口號");
private JTextField jTextField2 = new JTextField(30);
private JLabel jLabel3 = new JLabel("TQ終端IP");
private JTextField jTextField3 = new JTextField(30);
private JLabel jLabel4 = new JLabel("端口號");
private JTextField jTextField4 = new JTextField(30);
private JLabel jLabel5 = new JLabel("WM終端IP");
private JTextField jTextField5 = new JTextField(30);
private JLabel jLabel6 = new JLabel("端口號");
private JTextField jTextField6 = new JTextField(30);
private JButton jButton1 = new JButton("取消");
private JButton jButton2 = new JButton("確定");
private void showFrame(){
try {
File file = new File(iniPath);
System.out.println(file.getAbsolutePath());
properties.load(new FileInputStream(iniPath));
} catch (FileNotFoundException e) {
System.out.println("找不到該文件");
JOptionPane.showMessageDialog(null, "保存信息出錯!");
return;
} catch (IOException e) {
System.out.println("文件讀取錯誤");
JOptionPane.showMessageDialog(null, "保存信息出錯!");
return;
}
jTextField1.setText(properties.getProperty("UserLogin"));
jTextField2.setText(properties.getProperty("Userport"));
jTextField3.setText(properties.getProperty("TQterminal"));
jTextField4.setText(properties.getProperty("TQport"));
jTextField5.setText(properties.getProperty("VMterminal"));
jTextField6.setText(properties.getProperty("VMport"));
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextField1.setText(properties.getProperty("UserLogin"));
jTextField2.setText(properties.getProperty("Userport"));
jTextField3.setText(properties.getProperty("TQterminal"));
jTextField4.setText(properties.getProperty("TQport"));
jTextField5.setText(properties.getProperty("VMterminal"));
jTextField6.setText(properties.getProperty("VMport"));
}
});
jButton2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
properties.setProperty("UserLogin", jTextField1.getText());
properties.setProperty("Userport", jTextField2.getText());
properties.setProperty("TQterminal", jTextField3.getText());
properties.setProperty("TQport", jTextField4.getText());
properties.setProperty("VMterminal", jTextField5.getText());
properties.setProperty("VMport", jTextField6.getText());
try {
properties.store(new FileOutputStream(iniPath),"");
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("保存信息出錯");
JOptionPane.showMessageDialog(jFrame, "保存信息出錯!");
}
JOptionPane.showMessageDialog(jFrame, "保存成功!");
}
});
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jLabel1.setBounds(10, 40, 80, 30);
jTextField1.setBounds(100, 40, 80, 30);
jLabel2.setBounds(210, 40, 80, 30);
jTextField2.setBounds(300, 40, 80, 30);
jLabel3.setBounds(10, 80, 80, 30);
jTextField3.setBounds(100, 80, 80, 30);
jLabel4.setBounds(210, 80, 80, 30);
jTextField4.setBounds(300, 80, 80, 30);
jLabel5.setBounds(10, 120, 80, 30);
jTextField5.setBounds(100, 120, 80, 30);
jLabel6.setBounds(210, 120, 80, 30);
jTextField6.setBounds(300, 120, 80, 30);
jFrame.getContentPane().setLayout(null);
jFrame.getContentPane().add(jLabel1);
jFrame.getContentPane().add(jLabel2);
jFrame.getContentPane().add(jLabel3);
jFrame.getContentPane().add(jLabel4);
jFrame.getContentPane().add(jLabel5);
jFrame.getContentPane().add(jLabel6);
jFrame.getContentPane().add(jTextField1);
jFrame.getContentPane().add(jTextField2);
jFrame.getContentPane().add(jTextField3);
jFrame.getContentPane().add(jTextField4);
jFrame.getContentPane().add(jTextField5);
jFrame.getContentPane().add(jTextField6);
jButton1.setBounds(100,160,60,30);
jButton2.setBounds(230,160,60,30);
jFrame.getContentPane().add(jButton1);
jFrame.getContentPane().add(jButton2);
jFrame.setBounds(200, 200, 400, 300);
jFrame.setVisible(true);
}
public static void main(String[] args) {
new IniReader().showFrame();
}}
經測試,可用,正常。就是文件路徑你自己配好。
本回答由網友推薦
已贊過
已踩過<
你對這個回答的評價是?
評論
收起
總結
以上是生活随笔為你收集整理的java程序中my.ini_帮忙写个JAVA 读写ini配置文件小程序!!!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7如何启用监控功能
- 下一篇: java构建protobuf中的map,