84139

How does file creation work in Java

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:</b>

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(); } }

Answer1:

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("Path:" + f.getAbsolutePath());

To create it in a directory of your choosing:

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

Answer2:

newFile.createNewFile();

Answer3:

you could use newFile.createNewFile();

Recommend

  • POSIX O_DIRECT vs Windows FILE_FLAG_WRITE_THROUGH & FILE_FLAG_NO_BUFFERING
  • Calling python function with an unknown number of arguments [duplicate]
  • How to create file with help of P/Invoke to read boot sector and display .MBR?
  • C++(Serial Communicatio using the ) - How can i find out before hand, how many characters will be re
  • quiver not drawing arrows just lots of blue, matlab
  • Suppressing passwd when calling sqlplus from shell script
  • Zoom in and out of jPanel
  • Firefox Extension - Monitor refresh and change of tab
  • Saving Changes After In-App Purchase Has Been Purchased
  • Handling un-mapped Rest path
  • Paperclip, set path outside of rails root folder
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • What is Eclipse's Declaration View used for?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • output of program is not same as passed argument
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Trying to switch camera back to front but getting exception
  • vba code to select only visible cells in specific column except heading
  • Large data - storage and query
  • To display the title for the current loaction in map in iphone
  • Jquery - Jquery Wysiwyg return html as a string
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • R: gsub and capture
  • SVN: Merging two branches together
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • PHP: When would you need the self:: keyword?
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Memory offsets in inline assembly
  • unknown Exception android
  • Append folder name and increment by 1 using batch script
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?