开发者

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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜