开发者

specifying the location for file

开发者 https://www.devze.com 2023-04-11 15:36 出处:网络
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 fro

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

精彩评论

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