好吧,我在创建和编写Java文件方面做了大量工作。但是,我无法弄清楚为什么我的输出不会写,我的File file
无法找到,因为它是在我的主要方法中创建/读取的,你,是class
。
public class EloManip {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblFormat;
private JTextField textField_5;
private JLabel lblCase;
private JLabel label;
//Path p5 = Paths.get(System.getProperty("user.home"),"EloManip", "EloManipCases.log");
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/Peter/EloManipulation.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EloManip window = new EloManip();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public EloManip() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("Elo Manipulation Case Creator by Cavasi");
/*lblFormat = new JLabel("Format");
lblFormat.setBounds(32, 180, 307, 14);
frame.getContentPane().add(lblFormat);
*/
textField = new JTextField();
textField.setBackground(new Color(255, 255, 255));
textField.setBounds(173, 34, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblEnterName = new JLabel("Enter name:");
lblEnterName.setBounds(32, 37, 122, 14);
frame.getContentPane().add(lblEnterName);
JLabel lblEnterReason = new JLabel("Enter Reason:");
lblEnterReason.setBounds(32, 62, 104, 14);
frame.getContentPane().add(lblEnterReason);
textField_1 = new JTextField();
textField_1.setBackground(new Color(255, 255, 255));
textField_1.setBounds(173, 59, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JLabel lblEnterEvidence = new JLabel("Enter Evidence:");
lblEnterEvidence.setBounds(32, 87, 131, 14);
frame.getContentPane().add(lblEnterEvidence);
textField_2 = new JTextField();
textField_2.setBackground(new Color(255, 255, 255));
textField_2.setBounds(173, 84, 86, 20);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel lblEnterDatemmddyy = new JLabel("Enter Date(MM/DD/YY):");
lblEnterDatemmddyy.setBounds(32, 112, 136, 14);
frame.getContentPane().add(lblEnterDatemmddyy);
textField_3 = new JTextField();
textField_3.setBackground(new Color(255, 255, 255));
textField_3.setBounds(173, 109, 86, 20);
frame.getContentPane().add(textField_3);
textField_3.setColumns(10);
JLabel lblEnterLength = new JLabel("Enter Length:");
lblEnterLength.setBounds(32, 137, 122, 14);
frame.getContentPane().add(lblEnterLength);
textField_4 = new JTextField();
textField_4.setBackground(new Color(255, 255, 255));
textField_4.setBounds(173, 134, 86, 20);
frame.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBackground(new Color(255, 255, 255));
textField_5.setBounds(10, 207, 414, 20);
frame.getContentPane().add(textField_5);
textField_5.setColumns(10);
JButton btnNewButton = new JButton("Generate Elo Manipulation Case");
btnNewButton.setFont(new Font("Nirmala UI Semilight", Font.BOLD, 11));
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setBackground(Color.GREEN);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//lblFormat.setText(textField)
//JOptionPane.showMessageDialog(null, textField.getText() + " - " + textField_1.getText() + " - " + textField_2.getText() + " " + textField_3.getText() + " [" + textField_4.getText() + "]");
textField_5.setText(textField.getText() + " - " + textField_1.getText() + " - " + textField_2.getText() + " " + textField_3.getText() + " [" + textField_4.getText() + "]");
JOptionPane.showMessageDialog(null, "Successfully created Elo Manipulation Case!");
Writer output;
output = new BufferedWriter(new FileWriter(file, true));
output.append("New Line!");
output.close();
}
});
btnNewButton.setBounds(0, 238, 434, 23);
frame.getContentPane().add(btnNewButton);
lblCase = new JLabel("Case:");
lblCase.setBounds(10, 182, 352, 14);
frame.getContentPane().add(lblCase);
label = new JLabel("");
label.setBounds(269, 11, 155, 163);
frame.getContentPane().add(label);
Image img = new ImageIcon(this.getClass().getResource("/badlionsmall.png")).getImage();
label.setIcon(new ImageIcon(img));
}
}
文件无法解析为变量,因为它是在我的main方法中创建/检查的,但我需要写入.txt
文件的操作不在主要内容中方法,而是初始化方法。我该怎么办?
答案 0 :(得分:1)
为什么我的文件没有附加?
如果我错了,请纠正我......但我认为这个问题实际上是关于编译错误的。问题是file
方法中的局部变量main
不在您的监听器范围内。
解决方案是移动file
的声明。最简单的方法是使其成为EloManip
的静态(私有,最终)字段。
(将状态置于静态中通常不是一个好主意,但如果状态实际上是一个常量,并且它本地化为单个类及其内部类,则没有太大的危害。但是,有更多的OO处理这个问题的方法......)
答案 1 :(得分:0)
您实际上声明了两个FileWriter变量
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
Writer output;
output = new BufferedWriter(new FileWriter(file, true));
您是否尝试过仅使用一个?我怀疑第一个是清除你的内容,而不是第二个。
答案 2 :(得分:0)
这对我有用;
public static void main(String[] args) throws IOException {
File f = new File("test.txt");
try(FileWriter fw = new FileWriter(f, true)) {
fw.append("charsequence\r\n");
} catch(IOException e) {
e.printStackTrace();
}
}
请记住,只有关闭文件然后再次打开才能显示对文件的更改。