31

I want to create a file in a new directory using the relative path. Creating the directory "tmp" is easy enough.

However, when I create the file, it is just located in the current directory not the new one. The code line is below.

 File tempfile = new File("tempfile.txt"); 

Have tried this also:

 File tempfile = new File("\\user.dir\\tmp\\tempfile.txt"); 

Clearly I'm misunderstanding how this method works. Your assistance is greatly appreciated.

EDIT: added currently used code line as well as the one I think might work for a relative path to clear up confusion.

2
  • 1
    The code above uses an absolute path: \user.dir\tmp\tempfile.txt. I don't see how this file could be created in the current directory. Post the relevant code, explain us what you expect it to do, and what it does instead. Commented Mar 11, 2012 at 19:50
  • 1
    "..using the relative path." Relative to what? The application? The package of the class? The relativistic observer? Note that a) That is a constructor, not a method. b) user.dir will not be automatically expanded. c) Programming by magic rarely works, try reading the documentation. Commented Mar 11, 2012 at 19:53

4 Answers 4

41
File dir = new File("tmp/test"); dir.mkdirs(); File tmp = new File(dir, "tmp.txt"); tmp.createNewFile(); 

BTW: For testing use @Rule and TemporaryFolder class to create temp files or folders

Sign up to request clarification or add additional context in comments.

1 Comment

Just as a heads up (for you may not know this) but Sun, er Oracle, has very good API docs. Once you learn to understand and navigate them, they can be a massive time saver. For instance, the solution to your specific problem could have been figured out if you had looked at the various constructors available to the users of the File class: docs.oracle.com/javase/6/docs/api/java/io/File.html
5

You can create paths relative to a directory with the constructors that take two arguments: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

For example:

File tempfile = new File("user.dir/tmp", "tempfile.txt"); 

By the way, the backslash "\" can be used only on Windows. In almost all cases you can use the portable forward slash "/".

6 Comments

What happened to File.separator?
"In almost all cases you should use the portable forward slash "/"." In every case you should use either the File constructor that accepts a File (parent) & String (file name) or use System.getProperty("file.separator").
@Manish It should be all lower case.
@AndrewThompson I was referring to the separator property of the File class.
Oh, that separator! Given it is a constant, I would've expected it to be File.SEPARATOR - bloody Sun. ;) Thanks for clarifying.
|
2
String routePath = this.getClass().getClassLoader().getResource(File.separator).getPath(); System.out.println(routePath); /*for finding the path*/ String newLine = System.getProperty("line.separator"); BufferedWriter bw = new BufferedWriter(new FileWriter(new File(routePath+File.separator+".."+File.separator+"backup.txt"), true)); /*file name is backup.txt and this is working.*/ 

Comments

0

Let say you have "Local-Storage" on your project folder and you want to put a text or whatever file using file write.

 File file = new File(dir,fileName ); //KEY IS DIR ex."./local-storage/" and fileName='comp.html' // if file doesnt exists, then create it if ( ! file.exists( ) ) { file.createNewFile( ); } FileWriter fw = new FileWriter( file.getAbsoluteFile( ) ); BufferedWriter bw = new BufferedWriter( fw ); bw.write( text ); 

1 Comment

Why not closing the BufferedWriter?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.