2011-10-07 66 views
0

我需要给出用户存储文件的默认位置。像“/用户/用户名/桌面”。如何给文件的位置?下面的代码会从我运行的位置生成文件。指定文件的位置

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

回答

0

您需要的路径添加到文件名:

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

谢谢你,这是为我工作。 – Chandu

1

您可以在用户的​​主目录中获取具有:

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

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

也许从那里?

(我个人会避免使用任何PrintWriterFileWriter,顺便说一句 - PrintWriter燕子异常,FileWriter没有让你指定要使用的编码)