Search code examples
javafiletemporary-files

File.createNewFile() failing in java (Ubuntu 12.04)


I am trying to createNewFile() in java.I have written down the following example.I have compiled it but am getting a run time error.

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


public class CreateFileExample
{
    public static void main(String [] args)
    {


            try
            {
                    File file = new File("home/karthik/newfile.txt");

                    if(file.createNewFile())
                    {
                            System.out.println("created new fle");
                    }else
                    {
                            System.out.println("could not create a new file");
                    }
            }catch(IOException e )
            {
                    e.printStackTrace();
            }

    }

}

It is compiling OK.The run time error that I am getting is

java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:947)
    at CreateFileExample.main(CreateFileExample.java:16)

Solution

  • some points here

    1- as Victor said you are missing the leading slash

    2- if your file is created, then every time you invoke this method "File.createNewFile()" will return false

    3- your class is very platform dependent (one of the main reasons why Java is powerful programming language is that it is a NON-PLATFORM dependent), instead you can detect a relative location throw using the System.getProperties() :

        // get System properties :
        java.util.Properties properties = System.getProperties();
    
        // to print all the keys in the properties map <for testing>
        properties.list(System.out);
    
        // get Operating System home directory
        String home = properties.get("user.home").toString();
    
        // get Operating System separator
        String separator = properties.get("file.separator").toString();
    
        // your directory name
        String directoryName = "karthik";
    
        // your file name
        String fileName = "newfile.txt";
    
    
        // create your directory Object (wont harm if it is already there ... 
        // just an additional object on the heap that will cost you some bytes
        File dir = new File(home+separator+directoryName);
    
        //  create a new directory, will do nothing if directory exists
        dir.mkdir();    
    
        // create your file Object
        File file = new File(dir,fileName);
    
        // the rest of your code
        try {
            if (file.createNewFile()) {
                System.out.println("created new fle");
            } else {
                System.out.println("could not create a new file");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    this way you will create your file in any home directory on any platform, this worked for my windows operating system, and is expected to work for your Linux or Ubuntu as well