Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>i was just think is there a easier way, for example i already have the Jfilechoser open a "save as box" when the "submit" button is pressed so is there a easier way to create the file (saving the gui infomation in a txt file) ?</p> </blockquote> <p>This is a continuation on your <a href="https://stackoverflow.com/questions/4317483/java-item-listener">previous question</a>. You should just get the selected file and write to it with help of any <a href="http://download.oracle.com/javase/6/docs/api/java/io/Writer.html" rel="nofollow noreferrer"><code>Writer</code></a>, like <a href="http://download.oracle.com/javase/6/docs/api/java/io/PrintWriter.html" rel="nofollow noreferrer"><code>PrintWriter</code></a>.</p> <pre><code>File file = fileChooser.getSelectedFile(); PrintWriter writer = new PrintWriter(file); try { writer.println(txtField1.getText().trim()); writer.flush(); } finally { writer.close(); } </code></pre> <p>Don't overcomplicate by creating a <code>new File()</code> on a different location and calling <code>File#createFile()</code>. Just writing to it is sufficient.</p> <h3>See also:</h3> <ul> <li><a href="http://download.oracle.com/javase/tutorial/essential/io/" rel="nofollow noreferrer">Java IO tutorial</a></li> </ul> <hr> <p><strong>update</strong> here's an <a href="http://sscce.org" rel="nofollow noreferrer">SSCCE</a>, you can just copy'n'paste'n'compile'n'run it.</p> <pre><code>package com.example; import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.swing.JFileChooser; public class Test { public static void main(String[] args) throws IOException { JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); PrintWriter writer = new PrintWriter(file); try { writer.println("Hello"); writer.flush(); } finally { writer.close(); } Desktop.getDesktop().open(file); } } } </code></pre>
 

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