所以我只是练习我的java编程,并希望利用它为自己构建一个小应用程序来保存我的工作简历,以后可以创建预算和类似的东西。现在转到代码本身,所以我查找了许多方法来编写java中的文件,我已经成功地这样做了,问题是我使用的相同代码工作正常和花花公子......没有&# 39;在代码的特定部分工作,并使用我的逻辑和当时代码正在做什么的知识,我无法理解为什么它不工作所以我需要有人帮助审查这个并看看是否他们抓到了我没见过的东西。
package jobinfo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login implements ActionListener
{
private JFrame frame;
private JButton loginB, newUserB;
private JTextField JTFUsername;
private JPasswordField JPFPassword;
public Login()
{
frame = new JFrame("JobInfo - Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500,500);
frame.setLayout(null);
newUserB = new JButton("New User");
newUserB.addActionListener(this);
newUserB.setBounds(40,280,100,30);
loginB = new JButton("Login");
loginB.addActionListener(this);
loginB.setBounds(180,280,100,30);
JLabel JLUsername = new JLabel("Username:");
JLUsername.setBounds(100,100,150,30);
JTFUsername = new JTextField();
JTFUsername.setToolTipText("Enter your username");
JTFUsername.setBounds(JLUsername.getBounds().x +70,JLUsername.getBounds().y,150,30);
JLabel JLPassword = new JLabel("Password:");
JLPassword.setBounds(100,150,150,30);
JPFPassword = new JPasswordField();
JPFPassword.setToolTipText("Enter your password");
JPFPassword.setBounds(JLPassword.getBounds().x+ 70, JLPassword.getBounds().y, 150, 30);
frame.add(loginB);
frame.add(newUserB);
frame.add(JLUsername);
frame.add(JTFUsername);
frame.add(JLPassword);
frame.add(JPFPassword);
frame.setVisible(true);
System.out.println("Login Page");
所以我在这里使用并成功写入文件。在我的日志文件中,我可以看到Application已成功启动语句。
try{
FileWriter fw = new FileWriter(
"/Users/credant/Desktop/Programming/JILogs/AppLogs.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("--> Application has Launched successfully!");
bw.newLine();
bw.close();
}catch(Exception b)
{
JFrame Dframe = new JFrame();
JOptionPane.showMessageDialog(Dframe,
b.getMessage(),"App Crashed", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == loginB)
{
try{
所以这就是问题所在,我做的与第一次写入文件完全相同,但在检查我的AppLogs.txt时,我从未看到语句"验证登录凭据&#34 ;但是在IDE控制台中,我确实得到了一个"更新了AppLogs.txt"它的代码在文件写入之后出现.....这是怎么可能的,我唯一的猜测是文件写入失败并且捕获它没有捕获它,所以就像我猜的那样抛出无声错误。虽然我从来没有听说过这样的事情。下面是另一个循环漏洞,如果您要运行此代码,您将看到一个登录页面,只有输入错误的凭据才会出现神秘错误,但如果您输入正确的凭据,则日志文件会更新,我们会移动正常......
FileWriter fw = new FileWriter(
"/Users/credant/Desktop/Programming/JILogs/AppLogs.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("--> Verifying Login Credentials");
bw.newLine();
System.out.println("Updated AppLogs.txt");
boolean usernam = false;
boolean passwor = false;
Scanner scan = new Scanner(new File("/Users/credant/Desktop/Programming/JobInfo/AccountDatabase/Accounts.txt"));
while(scan.hasNextLine())
{
try{
String line = scan.nextLine();
if(line.isEmpty())
{
}
else
{
String checkUser[] = line.split(":");
if(checkUser[0].equals("Username"))
{
checkUser[1] = checkUser[1].substring(1);
if(checkUser[1].equals(JTFUsername.getText()))
{
bw.write("--> Username is correct");
bw.newLine();
usernam = true;
}
else
{
bw.write("--> Username is invalid, Displaying Incorrect message");
bw.newLine();
usernam = false;
}
}
else if(checkUser[0].equals("Password"))
{
checkUser[1] = checkUser[1].substring(1);
String password = new String(JPFPassword.getPassword());
if(checkUser[1].equals(password))
{
bw.write("--> Password is correct!");
bw.newLine();
passwor = true;
}
else
{
bw.write("--> Password is invalid, Displaying Incorrect message");
bw.newLine();
passwor = false;
}
}
}
}catch(Exception b)
{
JFrame Dframe = new JFrame();
JOptionPane.showMessageDialog(Dframe,
b.getMessage(),"App Crashed", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
if(usernam == true)
{
if(passwor == true)
{
bw.write("--> Authorization completed, retrieving JobInfo Account page");
bw.newLine();
bw.write("--> Logged in as:"+ JTFUsername.getText());
bw.newLine();
bw.close();
frame.setVisible(false);
}
else
{
bw.write("--> Password is invalid, displaying invalid message.");
bw.newLine();
JLabel invalid = new JLabel("Invaild Credentials, Please try again.");
invalid.setBounds(100, 310 , 100, 30);
invalid.setForeground(Color.red);
frame.add(invalid);
}
}
else if(usernam == false)
{
bw.write("--> Username is invalid, displaying invalid message.");
bw.newLine();
JLabel invalid = new JLabel("Invaild Credentials, Please try again.");
invalid.setBounds(100, 310 , 100, 30);
invalid.setForeground(Color.red);
frame.add(invalid);
}
}catch(Exception b)
{
JFrame Dframe = new JFrame();
JOptionPane.showMessageDialog(Dframe,
b.getMessage(),"App Crashed", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
else if(e.getSource() == newUserB)
{
try{
FileWriter fw = new FileWriter(
"/Users/credant/Desktop/Programming/JILogs/AppLogs.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("--> Setting up to add a new User");
bw.newLine();
bw.close();
frame.setVisible(true);
AccountCreation createAccount = new AccountCreation();
createAccount.createAccount();
}catch(Exception b)
{
JFrame Dframe = new JFrame();
JOptionPane.showMessageDialog(Dframe,
b.getMessage(),"App Crashed", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
我不确定什么是错的,但是任何建议都会有所帮助,我也知道部分代码是非常错误的编程,我将进行改造,使用线程,什么不是什么有助于某些条件,如果有一个写入文件的方式较短,请随时提出建议。
答案 0 :(得分:0)
每当您写入文件时都需要使用flush()
方法:
例如更改:
BufferedWriter bw = new BufferedWriter(fw);
bw.write("--> Application has Launched successfully!");
bw.newLine();
bw.close();
为:
BufferedWriter bw = new BufferedWriter(fw);
bw.write("--> Application has Launched successfully!");
bw.newLine();
bw.flush(); //add this line
bw.close();
最佳做法是flush
close
之前的buffer
。
但是,如果您突然要求关闭您的应用程序,您可能在撰写后立即flush
。