1

When I use the following code to create file, it doesn't output a visible file.It doesn't give any exception. In the following code output is exist. That means file is actually there exist. But I can't visible. Actually what is going on here?

File file= new File("/folder/abc.txt");
if(file.exist)
   System.out.println("exist");
Gayan Fernando
  • 581
  • 1
  • 7
  • 23
  • 1
    The class `File` might perhaps have been better named `FilePath`. Its constructor creates an object that may point to a file, or not. Most of its methods are about file path manipulation, not actual files. – Patricia Shanahan Nov 28 '13 at 13:39
  • But using the following coding it actually creats a file. What is the difference between two of these? File file= new File("folder//abc.txt"); – Gayan Fernando Nov 28 '13 at 13:46
  • Did you try to delete the file before you run the code? – nio Nov 28 '13 at 13:55

8 Answers8

6
    File file= new File("/folder/abc.txt");

NEVER creates an actual file. There are two ways to create a file:

  1. Invoke the createNewFile() method on a File object. For example:

    File file = new File("foo"); // no file yet
    file.createNewFile(); // make a file, "foo" which
    // is assigned to 'file'
    
  2. Create a Writer or a Stream. Specifically, create a FileWriter, a PrintWriter, or a FileOutputStream. Whenever you create an instance of one of these classes, you automatically create a file, unless one already exists, for instance

    File file = new File("foo"); // no file yet
    PrintWriter pw = new PrintWriter(file); // make a PrintWriter object AND
    // make a file, "foo" to which
    // 'file' is assigned, AND assign
    // 'pw' to the PrintWriter
    
sergeyan
  • 1,173
  • 1
  • 14
  • 28
4

It's a file abstraction class, this doesn't create any file yet. From documentation:

An abstract representation of file and directory pathnames.

You can do lot more than creating a new file, for example checking if such file or directory exist.

nio
  • 5,141
  • 2
  • 24
  • 35
  • Check out File.createTempFile method documentation and search for temporary file java questions, check for example this one: http://stackoverflow.com/questions/876816/open-temp-file-in-java – nio Nov 28 '13 at 13:47
4

Creating a File instance does not create a File on your file system.

You need to call a method of that instance to create the file on the file system

File f = new File("/folder/myfile");
if(!f.exists){
   f.createNewFile();
}
Frederic Close
  • 9,389
  • 6
  • 56
  • 67
3

As per Java docs,

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

creates only a instance. Actual file is created by using file.createNewFile();

codingenious
  • 8,385
  • 12
  • 60
  • 90
2

You are just creating a java object, you need to use:

File file= new File("/folder/abc.txt");
file.createNewFile();
Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20
2

For good practice check if file exists if not then create new one.

 File file= new File("/folder/abc.txt");
 if(!file.exists())
      file.createNewFile();
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
2
    File file= new File("/folder/abc.txt");

creates java File object, not real file. to create real file call:

    file.createNewFile();

or use Stream. For example:

     FileOutputStream stream = new FileOutputStream(file);
whiteErru
  • 275
  • 1
  • 5
  • 13
0

Just because you create an instance of java.io.File class, that file on the filesystem won't be created instantly. You have to take steps to actually create it. You will find information easily on this.

gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46