0

I want to make a program that you can email to someone and they can run it. Right now my code for making a file is like this:

File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();

But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?

4

2 回答 2

1

完全关闭路径,它将使用项目的目录。改变

File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");

File f = new File("axmlfile.xml");
于 2013-07-29T15:41:07.997 回答
0

我通常将这样的代码用于临时文件存储,这样当应用程序完成时它会被清理。如果需要,您可以允许用户保存文件的版本或将其移动到永久位置。

try{
   //create a temporary file
   File temp = File.createTempFile("axmlfile", ".xml"); 
   System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
   e.printStackTrace();
}
于 2013-07-29T15:42:00.007 回答