所以我试图找出如何在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程序员,我是新手,所以如果我错过了一个简单的解决方案,我很抱歉。
答案 0 :(得分:3)
您需要导入java.io.FileWriter