gpt4 book ai didi

java - 找不到符号 - 类 FileWriter。怎么修?

转载 作者:行者123 更新时间:2023-11-29 05:11:02 25 4
gpt4 key购买 nike

所以我试图找出如何在 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 程序员,我是这方面的新手,所以如果我错过了一个简单的解决方案,我很抱歉。

最佳答案

你需要导入java.io.FileWriter

关于java - 找不到符号 - 类 FileWriter。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com