Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of File is created when we create new File without the extension?

In java, when we create a file, we create files using the name of the extension. For example :

File file = new File("D:/light.txt");

I would like to know what type of file format do we get when we create a file without the file extension type. For example :

File file = new File("D:/light");
like image 548
Chit Khine Avatar asked May 10 '16 05:05

Chit Khine


2 Answers

This answer assumes you're doing more than just creating a File object - that you're actually creating a file on the file system. (A File object is just a logically representation of a file system entry which may or may not exist.) If you're really just creating a File object, read EJP's answer - at that point, you've basically just got a name. That doesn't have a "type" or a "format".

The extension is just part of the name. The operating system may try to use that to display a different icon, or launch a specific application when you double-click on the icon, or whatever - but it's really just part of the name.

Fundamentally, a file consists of:

  • The name you specify when you create it
  • The bytes you write in it
  • Metadata such as access control

Unless you deliberately add metadata, it's typically just inherited (default permissions etc).

You can write any data in any file - just because a file has an extension of .txt doesn't mean it's definitely a text file. It could have content which is actually MP3-encoded audio data, for example. Whether the OS uses the file extension or the content to work out what to do with the file is up to the OS.

like image 184
Jon Skeet Avatar answered Oct 17 '22 01:10

Jon Skeet


What type of File is created when we create new File without the extension?

No file is created at all, and there is only one type of File.

In java, when we create a file, we create files using the name of the extension.

Or not.

For example: File file = new File("D:/light.txt");

I would like to know what type of file format do we get when we create a file without the file extension type.

You don't. You don't get any file format at all, because you don't get a file: only a File object in memory.

For example: File file = new File("D:/light");

You can produce all the examples you want, but no file is created, and no file format.

In any case Java doesn't care about filename extensions. Your operating system might, but that's a different story.

like image 43
user207421 Avatar answered Oct 17 '22 00:10

user207421