1

I have a problem with creating an outputstream file.

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

It worked fine, until I made another method:

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

Previously, I put throws Exception at the start of each method that calls upon the

createprofile();

method (in which the output stream is). But now I get

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

Previously, I was wondering if there was another way to throw the exception: cannot implement actionPerformed(ActionEvent) in ActionListener But now I think that it would be better to somehow force the outputstream to make the file. I have googled multiple phrasings of this, but I do now know how to do this... the things I found did not work either.

4

2 回答 2

1

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如果文件不存在,则能够创建文件,但如果路径不存在或您没有足够的权限写入指定位置或任何其他可能的问题,则可能会出现问题...

于 2013-10-30T06:31:34.147 回答
0

你得到一个类型不匹配。ActionListener接口的方法actionPerformed包含子句throws Exception,因此您不能在覆盖的方法中包含子句。一个简单的解决方法是捕获任何Exception抛出的东西,然后将其重新抛出为RuntimeException. 由于RuntimeExceptions 未选中,因此您无需将其包含在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
  }
}

如果可能的话,通常最好实际处理异常,但如果你只是想查看异常(例如用于调试),那么我会说将它包装在 a 中RuntimeException并重新抛出它比仅仅添加throws Exception到最后更干净一点你所有的方法签名。如果您可以Exceptioncatch块中的类型缩小到您期望的实际异常类型,那也会更好。

于 2013-10-30T06:29:50.580 回答