views:

84

answers:

4

I am trying to create a file using

File newFile = new File("myFile");

However no file called "myFile" is created. This is within a Web application Project i.e. proper form to be pakaged as a WAR but I am calling it as part of a main method (just to see how this works).

How can I make it so that a new file is created at a location relative to the current one i.e not have to put in an absolute path.

EDIT:

newFile.createFile();

Doesn't seem to work:

Here is the entire code:

import java.io.File;
import java.io.IOException;

public class Tester {

public static void main(String[] args) throws IOException{
    Tester test = new Tester();
    test.makeFile();
}

public void makeFile() throws IOException{
    File newFile = new File("myFile");
    newFile.createNewFile();
    }
}
+3  A: 

use newFile.createNewFile();

kukudas
You're fast! :) LOL
Lirik
Just lucky.. ;)
kukudas
+1 for being lucky and the good answer of course! :)
Lirik
+2  A: 

newFile.createNewFile();

Lirik
A: 

this would itself create a file in ur path.. how do u want it to be ?

Magesh
well I want to be able to control it relative to the current location where current location is the location of the java class.
Ankur
if you're inside a .war, then the current location of the java class might not be in the filesystem, it's a virtual path inside a jar archive.
nos
+2  A: 

In answer to your comment. The file will be created in the current directory of the process, unless you specifiy otherwise.

// new file in current directory
File f = new File("yourFile");
f.createNewFile();
System.out.println(f.getAbsolutePath()

To create it in a directory of your choosing:

File f = new File("c:\\yourDirectory","yourFile");
f.createNewFile();
System.out.println(f.getAbsolutePath()
Gareth Davis