簡體   English   中英

如果文件不存在,如何使outputstream生成文件?

[英]How to make outputstream make a file if it doesn't exist?

我在創建outputstream文件時遇到問題。

OutputStream output = new FileOutputStream(username + ".txt");
byte buffer[] = data.getBytes();
output.write(buffer);
output.close();

效果很好,直到我提出另一種方法:

public void actionPerformed (ActionEvent e) //When a button is clicked
{
   if (e.getSource() == encrBtn)
   {
        menu.setVisible(false);
      createProfile();
      menu.setVisible(true);
   }
   else
   {
      if (e.getSource() == decrBtn)
      {
         menu.setVisible(false);
         viewProfile();
        menu.setVisible(true);
      }
      else
      {
         if (e.getSource() == exitBtn)
         {
            JOptionPane.showMessageDialog(null, "Goodbye!");
        System.exit(0);
         }
      }
   }
}

以前,我在每個調用

createprofile();

方法(輸出流在其中)。 但是現在我明白了

ProfileEncryption_2.java:54: error: actionPerformed(ActionEvent) in ProfileEncryption_2     cannot implement actionPerformed(ActionEvent) in ActionListener
public void actionPerformed (ActionEvent e) throws Exception //When a button is clicked
           ^
overridden method does not throw Exception

以前,我想知道是否還有另一種引發異常的方法: 無法在ActionListener中實現actionPerformed(ActionEvent),但是現在,我認為最好以某種方式強制輸出流來制作文件。 我已經用谷歌搜索了多個短語,但是現在我知道該怎么做了……我發現的東西也不起作用。

ActionListener接口未將其actionPerformed方法聲明為拋出任何類型的Exception ,因此無法更改此簽名。

您需要從方法內部捕獲和管理異常。

public void actionPerformed(ActionEvent e) //When a button is clicked
{
    if (e.getSource() == encrBtn) {
        menu.setVisible(false);
        try {
            createProfile();
        } catch (Exception exp) {
            exp.printStackTrace();
            JOptionPane.showMessageDialog(this, "Failed to create profile", "Error", JOptionPane.ERROR_MESSAGE);
        }
        menu.setVisible(true);
    } else {
        //...
    }
}

FileOutputStream能夠創建文件(如果文件不存在),但是如果路徑不存在或者您沒有足夠的權限寫入指定的位置或任何其他可能的問題,則可能會出現問題...

您遇到類型不匹配的情況。 ActionListener接口的actionPerformed方法包含throws Exception子句,因此您不能在您覆蓋的方法包含一個throws Exception子句。 一個簡單的解決方法是捕獲所有拋出的Exception ,然后將其重新拋出為RuntimeException 由於RuntimeException未被選中,因此您無需將其包括在throws子句中。

public void actionPerformed (ActionEvent e) //When a button is clicked
{
  try { // <-- Added try block
     if (e.getSource() == encrBtn)
     {
          menu.setVisible(false);
        createProfile();
        menu.setVisible(true);
     }
     else
     {
        if (e.getSource() == decrBtn)
        {
           menu.setVisible(false);
           viewProfile();
          menu.setVisible(true);
        }
        else
        {
           if (e.getSource() == exitBtn)
           {
              JOptionPane.showMessageDialog(null, "Goodbye!");
          System.exit(0);
           }
        }
     }
  }
  catch (Exception e) { // <-- Catch exception
    throw new RuntimeException(e); // <-- Re-throw as RuntimeException
  }
}

通常,如果可能的話,實際處理異常會更好,但是如果您只想查看異常(例如,用於調試),那么我會說將其包裝在RuntimeException並重新拋出它比僅添加throws Exception干凈一點。在所有方法簽名的末尾。 如果您可以將catch塊中的Exception類型縮小到您期望的實際異常類型,那也更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM