How to make outputstream make a file if it doesn&#

2019-08-04 17:31发布

问题:

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.

回答1:

The ActionListener interface does not declare it's actionPerformed method as throwing any type of Exception, you can not change this signature.

You need to catch and manage the exception from within the method.

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 is capable of creating the file if it does not exist, but may have issues if the path doesn't or if you don't have adequate permissions to write to the specified location or any number of other possible issues...



回答2:

You're getting a type mismatch. The ActionListener interface's actionPerformed method does not include a throws Exception clause, therefore you can't include one on the method you override. A simple fix is to catch any Exception thrown, and re-throw it as a RuntimeException. Since RuntimeExceptions are unchecked, you don't need to include it in the throws clause.

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

It's usually better to actually handle the exception if possible, but if you just want to see the exception (e.g. for debugging), then I'd say wrapping it in a RuntimeException and re-throwing it is a little cleaner than just adding throws Exception on the end of all your method signatures. It's also better if you can narrow the type of Exception in the catch block down to the actual exception types you're expecting.