Java 在Eclipse中写入文件的问题
我一直在自己做一个游戏,我有问题写一个简单的文本文件,我用它保存登录面板的数据。我正在使用java.swing创建所有内容。只是想知道是否有人能看到我的问题Java 在Eclipse中写入文件的问题,java,eclipse,file-writing,Java,Eclipse,File Writing,我一直在自己做一个游戏,我有问题写一个简单的文本文件,我用它保存登录面板的数据。我正在使用java.swing创建所有内容。只是想知道是否有人能看到我的问题 public class Main { private static String username; private static String password; public static void main(String[] args) { try { FileWriter fw = new FileWriter("d
public class Main {
private static String username;
private static String password;
public static void main(String[] args) {
try { FileWriter fw = new FileWriter("data.txt", true); //creates txt file for data
PrintWriter pw = new PrintWriter(fw); //creates writer to move data to txt file
}
catch (IOException e) { e.printStackTrace(); } //catches error with writing to txt file
getVariables(); //sets private variables to login info on text file
login(); //opens login frame
}
public static void login(){
JFrame frame = new JFrame(""); //creates JFrame for login window
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //FRAME SETTINGS
frame.setSize(180,140);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel(); //creates panel for login window
panel.setLayout(null);
JLabel usernameL = new JLabel("Username:"); //username label for login window
usernameL.setSize(100,20);
usernameL.setLocation(5,5);
JLabel passwordL = new JLabel("Password:"); //password label for login window
passwordL.setSize(100,20);
passwordL.setLocation(5,25);
final JTextField usernameField = new JTextField("",15); //username field for login window
usernameField.setSize(100,20);
usernameField.setLocation(70,5);
final JPasswordField passwordField = new JPasswordField("",15); //password field for login window
passwordField.setSize(100,20);
passwordField.setLocation(70,25);
JButton confirmLogin = new JButton("Log In"); //button to login
confirmLogin.setSize(80,20);
confirmLogin.setLocation(45,50);
confirmLogin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { //checks to see if login is correct
if(usernameField.getText().equals(username) && passwordField.getText().equals(password)){
gameMenu(); //calls game menu
}
else {
String message = "Your username or password is wrong!";
JOptionPane.showMessageDialog //displays panel stating login was incorrect
(new JFrame(), message, "Login Failed",
JOptionPane.ERROR_MESSAGE);
}
}
});
JButton createAccount = new JButton("Create Account"); //button to create account
createAccount.setSize(122,20);
createAccount.setLocation(25,75);
createAccount.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { //calls method to create account
createAccount();
}
});
panel.add(createAccount);
panel.add(passwordField);
panel.add(usernameField);
panel.add(usernameL); //add components to window
panel.add(passwordL);
panel.add(confirmLogin);
frame.add(panel);
frame.setVisible(true);
}
protected static void createAccount() {
final JFrame frame2 = new JFrame("Account Creation"); //creates window for account creation
frame2.setSize(290,110);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //window settings
frame2.setResizable(false);
JPanel panel2 = new JPanel();
panel2.setLayout(null); //creates panel for account creation window
JLabel usernameL2 = new JLabel("Username:");
usernameL2.setSize(100,20); //creates username label for window
usernameL2.setLocation(55,5);
JLabel passwordL2 = new JLabel("Password:"); //creates password label for window
passwordL2.setSize(100,20);
passwordL2.setLocation(55,25);
final JTextField usernameField2 = new JTextField("",15);
usernameField2.setSize(100,20); //creates username field for window
usernameField2.setLocation(120,5);
final JPasswordField passwordField2 = new JPasswordField("",15);
passwordField2.setSize(100,20); //creates password field for window
passwordField2.setLocation(120,25);
JButton confirm = new JButton("Confirm Creation");
confirm.setSize(130,20); //creates button to confirm account creation
confirm.setLocation(75,50);
confirm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try { FileWriter fw = new FileWriter("out.txt", false); //doesnt append (should overwrite data)
PrintWriter pw = new PrintWriter(fw);
pw.println(usernameField2.getText()); //gets info from textfield and pushes to data txt file
pw.println(passwordField2.getText()); //gets info from textfield and pushes to data txt file
pw.close(); //closes printwriter and saves data file
String message = "Account created succesfully!";
JOptionPane.showMessageDialog
(new JFrame(), message, "", //displays panel stating account creation was succesful
JOptionPane.INFORMATION_MESSAGE);
frame2.dispose();
}
catch (IOException g) { g.printStackTrace(); }
}});
panel2.add(confirm);
panel2.add(usernameL2);
panel2.add(passwordL2); //add components to account creation window
panel2.add(usernameField2);
panel2.add(passwordField2);
frame2.add(panel2);
frame2.setVisible(true);
}
public static void gameMenu(){
System.out.println("IT WORKS!"); //testing to make sure filewriting works
}
public static void getVariables(){
try {
FileReader fr = new FileReader("data.txt"); //gets ready to read from data file
BufferedReader br = new BufferedReader(fr);
String str;
for(int i = 0; i < 2; i++){
if((str = br.readLine()) != null && i == 0) { //reads first line and saves it
username = str;
}
if((str = br.readLine()) != null && i == 1) { //reads second line and saves it
password = str;
}
}
br.close(); //closes reader
} catch(IOException e) {
System.out.println("File not found.");
}
}
}
公共类主{
私有静态字符串用户名;
私有静态字符串密码;
公共静态void main(字符串[]args){
尝试{FileWriter fw=newfilewriter(“data.txt”,true);//为数据创建txt文件
PrintWriter pw=new PrintWriter(fw);//创建writer将数据移动到txt文件
}
catch(IOException e){e.printStackTrace();}//捕获写入txt文件的错误
getVariables();//将私有变量设置为文本文件上的登录信息
login();//打开登录框架
}
公共静态void登录(){
JFrame=newJFrame(“”;//为登录窗口创建JFrame
frame.setresizeable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//帧设置
框架。设置尺寸(180140);
frame.setLocationRelativeTo(空);
JPanel panel=new JPanel();//为登录窗口创建面板
panel.setLayout(空);
JLabel usernameL=新的JLabel(“用户名:”;//登录窗口的用户名标签
usernameL.setSize(100,20);
usernameL.setLocation(5,5);
JLabel passwordL=新的JLabel(“密码:”;//登录窗口的密码标签
密码设置大小(100,20);
密码设置位置(5,25);
final JTextField usernameField=新JTextField(“,15);//登录窗口的用户名字段
usernameField.setSize(100,20);
usernameField.setLocation(70,5);
final JPasswordField passwordField=new JPasswordField(“,15);//登录窗口的密码字段
设置大小(100,20);
设置位置(70,25);
JButton confirmLogin=新JButton(“登录”);//登录按钮
confirmLogin.setSize(80,20);
confirmLogin.setLocation(45,50);
confirmLogin.addActionListener(新ActionListener(){
public void actionPerformed(ActionEvent e){//检查登录是否正确
if(usernameField.getText().equals(用户名)和&passwordField.getText().equals(密码)){
gameMenu();//调用游戏菜单
}
否则{
字符串消息=“您的用户名或密码错误!”;
JOptionPane.showMessageDialog//显示面板,说明登录不正确
(新建JFrame(),消息“登录失败”,
JOptionPane.ERROR\u消息);
}
}
});
JButton createAccount=新建JButton(“创建帐户”);//创建帐户的按钮
createAccount.setSize(122,20);
createAccount.setLocation(25,75);
CreateCount.addActionListener(新ActionListener(){
public void actionPerformed(ActionEvent e){//调用方法创建帐户
createAccount();
}
});
panel.add(createAccount);
panel.add(密码字段);
panel.add(usernameField);
panel.add(usernameL);//将组件添加到窗口
panel.add(passwordL);
panel.add(confirmLogin);
框架。添加(面板);
frame.setVisible(true);
}
受保护的静态void createAccount(){
final JFrame frame2=新JFrame(“帐户创建”);//创建帐户创建窗口
框架2.设置大小(290110);
frame2.setLocationRelativeTo(空);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//窗口设置
frame2.可设置大小(false);
JPanel panel2=新的JPanel();
panel2.setLayout(null);//为帐户创建窗口创建面板
JLabel usernameL2=新的JLabel(“用户名:”);
usernameL2.setSize(100,20);//为窗口创建用户名标签
usernameL2.setLocation(55,5);
JLabel passwordL2=新的JLabel(“密码:”;//为窗口创建密码标签
密码L2.setSize(100,20);
密码L2.设置位置(55,25);
最终JTextField usernameField2=新的JTextField(“,15);
usernameField2.setSize(100,20);//为窗口创建用户名字段
usernameField2.setLocation(120,5);
最终JPasswordField passwordField2=新的JPasswordField(“”,15);
passwordField2.setSize(100,20);//为窗口创建密码字段
密码字段2.设置位置(120,25);
JButton confirm=新JButton(“确认创建”);
confirm.setSize(130,20);//创建按钮以确认帐户创建
确认设置位置(75,50);
confirm.addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
尝试{FileWriter fw=newfilewriter(“out.txt”,false);//不追加(应覆盖数据)
PrintWriter pw=新的PrintWriter(fw);
pw.println(usernameField2.getText());//从textfield获取信息并推送到数据txt文件
pw.println(passwordField2.getText());//从textfield获取信息并推送到数据txt文件
pw.close();//关闭printwriter并保存数据文件
字符串消息=“成功创建帐户!”;
JOptionPane.showMessageDialog
(新建JFrame(),消息,“,//显示说明帐户创建成功的面板
JOptionPane.INFORMATION(信息和消息);
frame2.dispose();
}
catch(iog异常){g.printStackTrace();}
}});
第2组。添加(确认);
panel2.add(usernameL2);
panel2.add(passwordL2);//将组件添加到帐户创建窗口
panel2.add(usernameField2);
面板2.添加(密码字段2);
框架2.添加(面板2);
frame2.setVisible(true);
}
公共静态void gameMenu(){
System.out.println(“它工作!”);//测试以确保文件写入工作正常
}
公共静态void getVariables(){
试一试{
Fi
for(int i = 0; i < 2; i++){
if(i == 0 && (str = br.readLine()) != null) {
username = str;
}
if(i == 1 && (str = br.readLine()) != null) {
password = str;
}
}
for(int i = 0; (str = br.readLine())!=null && i < 2;i++){
if(i == 0){
username = str;
}else{
password = str;
}
}