Chandu
Chandu

Reputation: 75

specifying the location for file

I need to give a default location for the user to store the file. like "/Users/username/Desktop". How to give the location for a file? Below code will generate the file in location from where I am running.

PrintWriter p = new PrintWriter(new FileWriter(tableName + "_insert.sql"));

Upvotes: 0

Views: 84

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1499770

You can get at the user's home directory with:

String directory = System.getProperty("user.home");

// Prefer this over string concatenation to create full filenames
File file = new File(directory, tableName + "_insert.sql");

Perhaps go from there?

(Personally I'd avoid using either PrintWriter or FileWriter, by the way - PrintWriter swallows exceptions, and FileWriter doesn't let you specify the encoding to use.)

Upvotes: 1

Thomas
Thomas

Reputation: 88707

You need to add the path to the filename:

 String yourPath = "/Users/username/Desktop/"; //get that somehow, e.g. by getting the user.dir system property, or hardcode
 PrintWriter p = new PrintWriter(new FileWriter(yourPath + tableName + "_insert.sql"));

Upvotes: 0

Related Questions