Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What does <code>MyFrame.ShowSaveDialog();</code> return? The key is to create a FileOutputStream with the appropriate constructor (the second parameter should be the boolean <code>true</code>) which will make it an appending FileOutputStream, and then construct your PrintStream using this FileOutputStream object.</p> <p>For instance, if showSaveDialog() (note that method and variable names should begin with lower case letters) returns the name of a file or a File object, you could do something like so:</p> <pre><code>try { File file = myFrame.showSaveDialog(); // if this method returns a File!!!!! FileOutputStream fos = new FileOutputStream(file, true); PrintStream printStream = new PrintStream(fos); //.... etc } catch(....) { // .... } </code></pre> <p><strong>Edit:</strong><br> To apply this to your posted code above, do something like so:</p> <pre><code> public static PrintStream showSaveDialog() { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Tekst filer", "txt"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(null); try { if (returnVal == JFileChooser.APPROVE_OPTION) { // ******* note changes below ***** File file = chooser.getSelectedFile(); FileOutputStream fos = new FileOutputStream(file, true); return new PrintStream(fos); } else { return null; } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "Ugyldig Fil!", "error", JOptionPane.ERROR_MESSAGE); } return null; } </code></pre> <p>The crux would be these lines here:</p> <pre><code> File file = chooser.getSelectedFile(); FileOutputStream fos = new FileOutputStream(file, true); return new PrintStream(fos); </code></pre> <p>The true in the FileOutputStream constructor creates a FileOutputStream that appends to the existing file. Please check out the FileOutputStream API for the details on this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload