简体   繁体   中英

java.io.File.createNewFile() method and file type

i know the method in java.io.File.createNewFile() will Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

What is the extention of file which it creates if i just pass it the path ?

try {
    String destPath = "/data/data/" + getPackageName() +
        "/databases";
    File f = new File(destPath);
    if (!f.exists()) 
    {               
        f.mkdirs();
        f.createNewFile();
          ........

    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

A file does not in reality have a type. It is just bits. Your application/os view it as having a type based on some rules, for example a file extension in the name.

JSON is just a format for text, which is just a convention for how bits map to characters (see character sets). The binary formats for video, imagery, etc. are again conventions/standards that have some encoding to represent whatever they are representing.

When you make a File you give it a name. The extension is just a part of that name. If you want a file named Foo that's a text file, then create a file Foo.txt .

If you don't provide an extension, there's nothing added. Files without extensions are still files. You could open them with any text editor like you could open .mp3 files with an editor, it just doesn't make very much sense.

So there will nothing be added to your path. The filename will just be your last segment of "/data/data/" + getPackageName() + "/databases" , in this case: databases .

you can create a file with no extension

File f;
f=new File("myfile");
if(!f.exists()){
  f.createNewFile();
}

see How to create a file in java without a extension

To create a file with extension

 f = new File("myFile.txt");

 // tries to create new file in the system
  bool = f.createNewFile();

As Other guys said if you do not explicitly provide an extension,it is created without an extension .

BTW I think if you would just run your code (or some example) you will see the result...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM