【发布时间】:2015-04-14 20:08:00
【问题描述】:
所以我试图找出如何在 Java 中永久存储数据,结果发现了这篇文章:Methods of storing permanent data in java
我找到了这个答案:
最简单的方法是使用 Properties 类。它存储键/值对,并可以将数据保存到属性文件中。这是一个工作示例:
Properties p = new Properties();
p.setProperty("johndoe.pin", "12345");
p.store(new FileWriter("myfile.properties", "");
and reading:
Properties p = new Properties();
p.load(new FileReader("myfile.properties", "");
The check will be done with the properties object:
public boolean isValid(String user, String pin) {
return pin.equals(p.getProperty(user + ".pin"));
}
这很容易,但当然没有加密。该文件以纯文本形式存储,包括 PIN。
我已经导入了 Java.util.Properties 包,我的其余代码正在运行,但我收到错误“找不到符号 - 类 FileWriter”
完整代码如下:
import java.util.Properties;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdminGUI extends JFrame {
Bank AccountHandler[];
int c = 0;
JLabel pinL;
JLabel balL;
JLabel nameL;
JTextField pinTF;
JTextField balTF;
JTextField nameTF;
JTextField resultTF;
JTextArea listTA;
JButton ClearB;
JButton ListB;
JButton CreateB;
JButton ExitB;
ClearButtonHandler clbHandler;
ListButtonHandler lbHandler;
CreateButtonHandler cbHandler;
ExitButtonHandler ebHandler;
Properties prop[];
public AdminGUI()
{
AccountHandler = new Bank[10];
setTitle("The Bank");
pinL = new JLabel("Enter desired PIN #: ", JLabel.RIGHT);
balL = new JLabel ("Enter the initial deposit: ", JLabel.RIGHT);
nameL = new JLabel ("Enter your name: ", JLabel.RIGHT);
pinTF = new JTextField(10);
balTF = new JTextField (10);
nameTF = new JTextField (10);
resultTF = new JTextField (10);
listTA = new JTextArea (5, 10);
ClearB = new JButton("Clear List");
clbHandler = new ClearButtonHandler();
ClearB.addActionListener(clbHandler);
ListB = new JButton("List Accounts");
lbHandler = new ListButtonHandler();
ListB.addActionListener(lbHandler);
CreateB = new JButton("Create");
cbHandler = new CreateButtonHandler();
CreateB.addActionListener(cbHandler);
ExitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
ExitB.addActionListener(ebHandler);
Container pane = getContentPane();
pane.setLayout(new GridLayout(6,2));
pane.add(pinL);
pane.add(pinTF);
pane.add(balL);
pane.add(balTF);
pane.add(nameL);
pane.add(nameTF);
pane.add(ClearB);
pane.add(resultTF);
pane.add(ListB);
pane.add(listTA);
pane.add(CreateB);
pane.add(ExitB);
setSize(600,400);
setVisible(true);
}
public class CreateButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
int pin;
double bal;
String name;
pin=Integer.parseInt(pinTF.getText());
bal=Double.parseDouble(balTF.getText());
name=nameTF.getText();
AccountHandler[c] = new Bank(pin, bal, name);
prop[c] = new Properties();
prop[c].setProperty("acct." + c, AccountHandler[c].pinString());
prop[c].store(new FileWriter("bankacct.properties", ""));
pinTF.setText("");
balTF.setText("");
nameTF.setText("");
resultTF.setText("Created. Welcome, " + name + ".");
c++;
}
}
public class ExitButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
public class ListButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
String list = "";
for(int i = 0; i != c; i++) {
list = list.concat(AccountHandler[i].toString());
}
listTA.setText(list);
}
}
public class ClearButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
listTA.setText("");
}
}
public static void main(String[] args) {
AdminGUI rectObject = new AdminGUI();
}
}
这是给我带来麻烦的部分:
AccountHandler[c] = new Bank(pin, bal, name);
prop[c] = new Properties();
prop[c].setProperty("acct." + c, AccountHandler[c].pinString());
prop[c].store(new FileWriter("bankacct.properties", ""));
我是一名初级 Java 程序员,而且我是新手,所以如果我错过了一个简单的解决方案,我很抱歉。
【问题讨论】:
-
你需要导入 FileWriter,就像 java 中的所有其他类一样。
标签: java windows-7 persistence bluej