S0urce C0ded
S0urce C0ded

Reputation: 116

How to set file path to src folder of project

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?

Upvotes: 0

Views: 9789

Answers (2)

StuPointerException
StuPointerException

Reputation: 7267

I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.

try{
   //create a temporary file
   File temp = File.createTempFile("axmlfile", ".xml"); 
   System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
   e.printStackTrace();
}

Upvotes: 0

Ryan
Ryan

Reputation: 257

Leave the path off entirely, it will use the directory of the project. Change

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

To

File f = new File("axmlfile.xml");

Upvotes: 1

Related Questions