Todd Goodfellow
Todd Goodfellow

Reputation: 11

how to create a file in a specific folder on desktop by default

I am trying to create a file with data inputted by user. But, I want that to be saved on a new folder on desktop every time user runs it. How can I do that?

package my.io;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;

public class BufferReader {

    public static void main(String args[]){

        System.out.print("Please enter your text!!: ");
        Scanner ip = new Scanner(System.in);
        String text = ip.nextLine();
        FileWriter fWriter = null;
        BufferedWriter writer = null;
        try {
          fWriter = new FileWriter("text.txt");
          writer = new BufferedWriter(fWriter); 
          writer.write(text);
          writer.newLine();
          writer.close();
          System.err.println("Iput of " + text.length() + " characters was saved on Desktop.");
          System.out.println("Text you have entered is:" + (ip.nextLine()));
        } catch (Exception e) {
            System.out.println("Error!");
        }

    }

}

Upvotes: 1

Views: 2930

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

First you need to know where the desktop is, you can use something like...

File desktop = new File(System.get("user.home") + File.separator + "Desktop");

Now, obviously, this is for Windows, for MacOS, you can follow something similar, but you'd need to verify the location from within the user.home context...

Now you have the desktop path, you could simply create a folder using something like...

File outputFolder = null;
do {
    outputFolder = new File(desktop, new SimpleDateFormat("yyyy-MM-dd HH:mm.ss").format(new Date()));
} while (outputFolder.exists());
if (!outputFolder.mkdirs()) {
    System.err.println("Failed to create output folder " + outputFolder);
}

Now, this is just creating folders with a timestamp with a second accuracy. It might be nice to give the directory a more meaningful name, but that comes down to needs...

The following example is a little more complicated, this basically lists all the directories in the desktop, which start with the predetermined predix.

It then loops throughs and determines the max number suffix and create a new directory with the next number in the sequence, this means that if you delete a directory, it won't try and overwrite an existing directory...

String prefix = "Test";

File[] folders = desktop.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.isDirectory()
                        && pathname.getName().startsWith(prefix);
    }
});

System.out.println(folders.length);

int max = 0;
for (File folder : folders) {
    String name = folder.getName();
    name = name.substring(prefix.length()).trim();
    System.out.println(name);

    max = Math.max(max, Integer.parseInt(name));
}
max++;

String suffix = String.format("%04d", max);

File output = new File(desktop, prefix + " " + suffix);
System.out.println(output);
if (!output.exists() && !output.mkdirs()) {
    System.out.println(output + " not created");
}

Upvotes: 1

Related Questions