specifying the location for file
I need to give a default location for the user to store the file. like "/Users/username/Desktop". 开发者_如何学CHow 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"));
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.)
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"));
精彩评论