2015-12-14 50 views
-2

好吧,我在創建和寫入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文件不在的主要方法,而是初始化方法。我該怎麼辦?

+0

局部變量不在它們出現的方法的範圍內,它們的作用範圍是它們出現的大括號('{}')。如果您希望能夠在類中的任何位置使用變量,你需要把它變成一個類變量。 – azurefrog

+4

如果您有新問題,請提出一個新問題。不要覆蓋這個。 –

+0

正如許多次所寫,一個新的問題屬於一個新的職位,而不是附加到這個職位。您可以點擊右上角的「提問」來創建一個新問題。 – Tom

回答

1

爲什麼我的文件不被追加?

糾正我,如果我錯了......但我認爲這個問題是關於編譯錯誤。問題是main方法中的局部變量file不在您的偵聽器中。

解決方法是移動file的聲明。最簡單的方法是將其設爲EloManip的靜態(私有,最終)字段。 (將狀態置入靜態通常不是一個好主意,但是如果狀態實際上是一個常量,並且它被本地化爲單個類及其內部類,則沒有太大的傷害。但是,還有更多處理這種OO方式......)

+0

我修正了它,但是現在我的文本被寫入了它,覆蓋了一次,然後我按下了我的按鈕,現在檢查我的線程 – Cavasi

+2

您通過刪除我的答案引用的問題來「破壞」您的問題。如果您希望我回答您的後續行動,請將原始代碼恢復並請求您的後續處理爲EDIT或作爲新問題。開始恢復您最後的編輯... –

+2

@Cavasi請不要改變你的問題。它使答案無效。 – Emz

0

你實際上聲明兩個變量FileWriter的

FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
    BufferedWriter bw = new BufferedWriter(fw); 
    Writer output; 
    output = new BufferedWriter(new FileWriter(file, true)); 

你只使用一個嘗試?我懷疑第一個是清理你的內容,而不是第二個。

+0

是的,這不是[正在工作 – Cavasi

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(); 
    } 
} 

請記住,更改文件,如果你關閉它,然後再次打開它纔會顯示。

+0

*「請記住,只有當您關閉並再次打開文件時,文件的更改纔會顯示。」* - 不正確。一個'flush()'也可以工作,並且你不必再次打開這個文件(在這個應用程序中),以使這些變化顯示在另一個應用程序中。 –

+0

如果他在諸如記事本_before_的程序中打開該文件,則刷新該流,然後否,這些更改將不可見。 –

+0

這是正確的。如果他在刷新或關閉之前在另一個程序中打開它。但是他不需要在此應用程序或另一個應用程序中再次打開它,以便可以看到更改。沖洗或關閉就足夠了。 (不適用於記事本...但對於在啓動時不讀取整個文件的其他應用程序)。 –

相關問題