2015-06-21 100 views
1

我有一個用戶密碼應用程序寫入文件 - 您在文本框中輸入信息,單擊一個按鈕,它應該寫入創建的文件的下一行。更新文本框的值

有兩個問題:

  1. 文件只寫數據
  2. 的一行它只是你進入最後寫道。我試過validate()repaint()但沒有什麼區別。
public class Main { 

public JFrame frame; 
public JTextField textField; 
public JTextField textField_1; 
public JTextField textField_2; 
public JTextField textField_3; 

public static void main(String[] args) { 

    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Main window = new Main(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 


} 

public Main() { 
    initialize(); 
} 

private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 588, 156); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JButton btnNewButton = new JButton("Add Record"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      openFile(); 
      printFormat(); 
      addRecords(); 
      closeFile(); 

     } 
    }); 
    btnNewButton.setBounds(237, 83, 91, 23); 
    frame.getContentPane().add(btnNewButton); 

    textField = new JTextField(); 
    textField.setColumns(10); 
    textField.setBounds(10, 44, 128, 20); 
    frame.getContentPane().add(textField); 

    textField_1 = new JTextField(); 
    textField_1.setColumns(10); 
    textField_1.setBounds(148, 44, 128, 20); 
    frame.getContentPane().add(textField_1); 

    textField_2 = new JTextField(); 
    textField_2.setFont(UIManager.getFont("PasswordField.font")); 
    textField_2.setColumns(10); 
    textField_2.setBounds(286, 44, 128, 20); 
    frame.getContentPane().add(textField_2); 

    textField_3 = new JTextField(); 
    textField_3.setColumns(10); 
    textField_3.setBounds(424, 44, 128, 20); 
    frame.getContentPane().add(textField_3); 

    JLabel lblNewLabel = new JLabel("Username"); 
    lblNewLabel.setBounds(10, 19, 128, 14); 
    frame.getContentPane().add(lblNewLabel); 

    JLabel lblPassword = new JLabel("Email"); 
    lblPassword.setBounds(148, 19, 128, 14); 
    frame.getContentPane().add(lblPassword); 

    JLabel lblMojangPassword = new JLabel("Mojang Password"); 
    lblMojangPassword.setBounds(286, 19, 128, 14); 
    frame.getContentPane().add(lblMojangPassword); 

    JLabel lblEmailPassword = new JLabel("Email Password"); 
    lblEmailPassword.setBounds(424, 19, 128, 14); 
    frame.getContentPane().add(lblEmailPassword); 

} 


private Formatter x; 

public void openFile(){ 
    try{ 
     x = new Formatter("okey.fil"); 

    } 
    catch(Exception e){ 
     System.out.println("error get it right"); 
    } 
} 
public void printFormat(){ 
    x.format("%-25s %-25s %-25s %-25s \n", "Username", "Email", "Mojang Password", "Email Password"); 
} 
public String getUsername(){ 
    String username = textField.getText(); 
    return username; 
} 
public void addRecords(){ 
     x.format("%-25s %-25s %-25s %-25s \n", getUsername(),textField_1.getText(),textField_2.getText(),textField_3.getText()); 
     x.format("\n"); 
     resetFields(); 
    } 


public void resetFields(){ 
    textField.setText(""); 
    textField_1.setText(""); 
    textField_2.setText(""); 
    textField_3.setText(""); 
    textField.validate(); 
    textField_1.validate(); 
    textField_2.validate(); 
    textField_3.validate(); 
} 
public void closeFile(){ 
    x.close(); 
} 
} 
+0

驗證後,預計有什麼區別? – 2015-06-21 10:26:46

+0

我看到它是爲了更新您輸入的文本 –

+0

可能的重複[如何將文本附加到Java中的現有文件](http://stackoverflow.com/questions/1625234/how-to-append-text-to -an-existing-file-in-java) – Tom

回答

1

的問題是:你打開你的文件時執行的「btnNewButton」操作時都 - 該文件被java.lang.Formatter在「W」模式,這意味着打開了它的截在開始時只寫新行。您實際上將每行都寫入文件,但只剩下最後一行。

解決方案1:您只需打開一次Formatter(或FileOutputStream或類似的東西),然後在其中添加行。您可以調用flush()而不是close()來實際將數據寫入文件。

解決方案2:您仍然可以在操作時都重新打開文件,如果你需要它 - 但你必須打開它的「追加」模式 - How to append text to an existing file in Java

例如,您可以使用此構造函數(第二個參數必須是真的)http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream%28java.io.File,%20boolean%29

0

您可以使用

x = new Formatter(new FileOutputStream("okey.fil",true)); 
+0

你能否請你的答案給出解釋爲什麼這段代碼回答這個問題的解釋?僅限代碼答案[不鼓勵](http://meta.stackexchange.com/questions/148272),因爲他們沒有教導解決方案。 – DavidPostill