java软件如何优雅地写配置文件
.ini
.json
目前我使用的是txt方式,有些拙劣
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Dialog.ModalityType;
import java.awt.Dialog.ModalExclusionType;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.awt.event.ActionEvent;
public class HadoopSettingWin extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField txtHdpIPPort;
private JTextField txtHdpDst;
public static String txtHdpIPPortValue = getIPPORTFormTxt();
public static String txtHdpDstValue = getDIRPathFormTxt();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
HadoopSettingWin dialog = new HadoopSettingWin();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public HadoopSettingWin() {
setTitle("Hadoop Setting(Upload)");
setModalityType(ModalityType.TOOLKIT_MODAL);
setResizable(false);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 179);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[]{140, 48, 66, 0};
gbl_contentPanel.rowHeights = new int[]{21, 0, 0, 0};
gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPanel.setLayout(gbl_contentPanel);
{
JLabel lblHadoopIPPort = new JLabel("Hadoop IP_Port");
GridBagConstraints gbc_lblHadoopIPPort = new GridBagConstraints();
gbc_lblHadoopIPPort.insets = new Insets(0, 0, 5, 5);
gbc_lblHadoopIPPort.gridx = 0;
gbc_lblHadoopIPPort.gridy = 0;
contentPanel.add(lblHadoopIPPort, gbc_lblHadoopIPPort);
}
{
txtHdpIPPort = new JTextField();
txtHdpIPPort.setText(getIPPORTFormTxt());
txtHdpIPPortValue =txtHdpIPPort.getText();
GridBagConstraints gbc_txtHdpIPPort = new GridBagConstraints();
gbc_txtHdpIPPort.insets = new Insets(0, 0, 5, 0);
gbc_txtHdpIPPort.fill = GridBagConstraints.HORIZONTAL;
gbc_txtHdpIPPort.gridx = 2;
gbc_txtHdpIPPort.gridy = 0;
contentPanel.add(txtHdpIPPort, gbc_txtHdpIPPort);
txtHdpIPPort.setColumns(10);
}
{
JLabel lblHadoopDst = new JLabel("Hadoop Dst");
GridBagConstraints gbc_lblHadoopDst = new GridBagConstraints();
gbc_lblHadoopDst.insets = new Insets(0, 0, 5, 5);
gbc_lblHadoopDst.gridx = 0;
gbc_lblHadoopDst.gridy = 1;
contentPanel.add(lblHadoopDst, gbc_lblHadoopDst);
}
{
txtHdpDst = new JTextField();
txtHdpDst.setText(getDIRPathFormTxt());
txtHdpDstValue = txtHdpDst.getText();
GridBagConstraints gbc_txtHdpDst = new GridBagConstraints();
gbc_txtHdpDst.insets = new Insets(0, 0, 5, 0);
gbc_txtHdpDst.fill = GridBagConstraints.HORIZONTAL;
gbc_txtHdpDst.gridx = 2;
gbc_txtHdpDst.gridy = 1;
contentPanel.add(txtHdpDst, gbc_txtHdpDst);
txtHdpDst.setColumns(10);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtHdpIPPortValue = txtHdpIPPort.getText();
txtHdpDstValue = txtHdpDst.getText();
setIPPORTToTxt(txtHdpIPPortValue);
setDIRPathToTxt(txtHdpDstValue);
dispose();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
public static String getIPPORTFormTxt() {
String s = "";
String pathname = "HadoopIPPort.txt"; //
//
//
//
try (FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader)
) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
s = line;
Main.RecordSomethingTolog("getHadoopIPPORTFormTxt"+s);
}
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
public static String getDIRPathFormTxt() {
String s = "";
String pathname = "DirPath.txt";
try (FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader)
) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
s = line;
Main.RecordSomethingTolog("getHadoopDIRPathFormTxt"+s);
}
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
public void setIPPORTToTxt(String IPPORT) {
try {
File writeName = new File("HadoopIPPort.txt");
writeName.createNewFile();
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)
) {
out.write(IPPORT+"\r\n");
Main.RecordSomethingTolog("setHadoopIPPORTToTxt"+IPPORT);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setDIRPathToTxt(String DirPath) {
try {
File writeName = new File("DirPath.txt");
writeName.createNewFile();
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)
) {
out.write(DirPath+"\r\n");
Main.RecordSomethingTolog("setHadoopDIRPathToTxt"+DirPath);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}