好吧,我在創建和寫入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("https://stackoverflow.com/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));
}
}
文件不能被解析爲一個變量,因爲它正在創建/在我的主要方法檢查,但我的行爲需要被寫入.txt
文件不在的主要方法,而是初始化方法。我該怎麼辦?
局部變量不在它們出現的方法的範圍內,它們的作用範圍是它們出現的大括號('{}')。如果您希望能夠在類中的任何位置使用變量,你需要把它變成一個類變量。 – azurefrog
如果您有新問題,請提出一個新問題。不要覆蓋這個。 –
正如許多次所寫,一個新的問題屬於一個新的職位,而不是附加到這個職位。您可以點擊右上角的「提問」來創建一個新問題。 – Tom