0

I'm trying to get my submit button to save the GUI in a text file, I've made the GUI and the button listener ...etc but I'm having trouble making the method that saves the information from the GUI into a text file.

so far i have:

public void save() {

    File k1 = new File("documents/"+"newfile.txt");
    try {
       k1.createNewFile();
       FileWriter kwriter = new FileWriter(k1);

       BufferedWriter bwriter = new BufferedWriter(kwriter);
       bwriter.write(txtField1.getText().trim());
       bwriter.newLine();
       bwriter.close();

    } catch (IOException e) {
       e.printStackTrace();
    }
}

but it doesn't seem to work, nothing happens; is there anything I'm missing?

jasonlam604
  • 1,456
  • 2
  • 16
  • 25

5 Answers5

2

You're file is called .txt - perhaps insert a name in the first row:

File k1 = new File("documents/filename.txt");
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • Or mayby use flush() before close the file?? like bwriter.flush() – exiter2000 Nov 30 '10 at 22:55
  • Could you try writing out a text like this: `bwriter.write("just something");` - and removing the row `k1.createNewFile();`, since making a `FileWriter` will implicitly create the file. – Martin Algesten Nov 30 '10 at 22:55
  • You could also try to give an absolute path to the file. Since the working directory of your java process may be somewhere you're not expecting it to be. – Martin Algesten Nov 30 '10 at 22:56
  • `.close()` does an implicit flush. – Martin Algesten Nov 30 '10 at 22:57
  • sorry none of those work, except the absolute path (which i havent tried) how can i implement that? – mark philips Nov 30 '10 at 23:01
  • If you're on windows, change the path to something like `File k1 = new File("C:\test.txt");` If you're on a Mac something like `File k1 = new File("/Users/[your login name]/test.txt");` and on Linux `File k1 = new File("/home/[your login name]/test.txt");` – Martin Algesten Nov 30 '10 at 23:02
  • 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) ? – mark philips Nov 30 '10 at 23:10
1

You should be getting an error when running that code. The problem is that the document directory doesn't exist or it is not where you expected.

You can check for the parent directory with:

if(!k1.getParentFile().exists()){
    k1.getParentFile().mkdirs();
}

Alternatively you need to set the file to be a more precise location. org.apache.commons.lang.SystemUtils might be able to help you out here with user home.

pimaster
  • 1,907
  • 10
  • 11
0

My guess is that the file is being created, but not in the directory that you expect. Check the value of the user.dir system property and see what it shows. This is the current working directory of your JVM.

You may need to either:

  • Specify a full path in the code (as Martin suggested), e.g. new File("/home/foo/newfile.txt")
  • Change the working directory of the JVM. The way that you do this depends on how you are launching it (e.g. if you are running the java command directly from a CLI, then just switch directories first, if you are running from an IDE then change the launch configuration, etc.).

As far as I know, you cannot change the working directory at runtime (see this SO question).

Community
  • 1
  • 1
Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
  • or add a `System.out.println(k1.getAbsolutePath());` to quickly check where the file is written – andcoz Nov 30 '10 at 23:10
0

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) ?

This is a continuation on your previous question. You should just get the selected file and write to it with help of any Writer, like PrintWriter.

File file = fileChooser.getSelectedFile();
PrintWriter writer = new PrintWriter(file);
try {
    writer.println(txtField1.getText().trim());
    writer.flush();
} finally {
    writer.close();
}

Don't overcomplicate by creating a new File() on a different location and calling File#createFile(). Just writing to it is sufficient.

See also:


update here's an SSCCE, you can just copy'n'paste'n'compile'n'run it.

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

}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • im getting a few errors with the above code, 1) it doesnt like "writer"??? also it doesnt like "printWriter" and "fileChooser"??? – mark philips Nov 30 '10 at 23:48
  • says "can not find symbol for each of those – mark philips Nov 30 '10 at 23:49
  • You need to add the necessary `import` statements like `import java.io.PrintWriter;` and so on. The `fileChooser` is obviously the `JFileChooser` as given in your previous question. Are you using an IDE like Eclipse (which can add import statements automagically) or a text editor like notepad? Once you become familiar with Java basics, I can recommend an IDE. – BalusC Nov 30 '10 at 23:50
  • cheers mate, im using notepad++ but have just downloaded eclipe. thanks again – mark philips Nov 30 '10 at 23:57
  • I've added a copy'n'paste'n'compilable'n'runnable SSCCE to the answer. – BalusC Nov 30 '10 at 23:58
  • wow, that works! last question how do i get it to save my 23 fields (combo-box,text box, check boxes etc)? i know i have to replace the "writer.println("Hello");" with "writer.println txtfield1; " etc? – mark philips Dec 01 '10 at 00:13
  • Yes, you can just call `println()` multiple times to print a new line everytime. – BalusC Dec 01 '10 at 00:16
  • slight problem,i get the following "javax.swing.JTextField[,137,77,122x20,layout=javax" etc when my code is "writer.println (txtfield1);" if i dont use the brackets i get "package writer does not exist" on writer. any idea what im doing wrong? – mark philips Dec 01 '10 at 00:27
  • You need to print the textfield contents which you can obtain by `getText()`, like as you did in your question, not the textfield component. – BalusC Dec 01 '10 at 00:34
0

Your code is working for me with minor change.

As a debugging process, I would completely delete the directory path and try to use a file only..

instead of

File k1 = new File("documents/"+"newfile.txt");

use

File k1 = new File("newfile.txt");

Check where your file is generated and then create directory there..

Good luck!!

exiter2000
  • 548
  • 5
  • 14