mark philips
mark philips

Reputation: 25

Using Java unable to save to text file

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?

Upvotes: 0

Views: 233

Answers (5)

BalusC
BalusC

Reputation: 1109432

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

}

Upvotes: 0

exiter2000
exiter2000

Reputation: 548

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

Upvotes: 0

pimaster
pimaster

Reputation: 1967

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.

Upvotes: 1

Matt Solnit
Matt Solnit

Reputation: 33572

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

Upvotes: 0

Martin Algesten
Martin Algesten

Reputation: 13620

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

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

Upvotes: 2

Related Questions