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

3

7

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");
Wittie answered 10/5, 2016 at 5:44 Comment(7)
A file without extension.Opus
A file's type is determined by its contents, not its name or extension.Neumark
I'm voting to close this question as off-topic because it's not related to programming.Neumark
Files by themselves don't have formats. The file format only exists because the program reading it interprets the data in one way or another.Carrero
Its interesting. Try it and see yourself :)Selection
This question is relevant for Windows only. Appropriate Tag should be addedCayes
The answer here is entirely system dependent. Some systems (e.g. most Eunuchs) have no concept of file extensions at all.Olcott
B
4

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.

Budweis answered 10/5, 2016 at 6:11 Comment(4)
Does it mean that if we create a .txt file and write an bitmap info into it, it will be the image file even though the extension states it to be a text?Wittie
@ChitKhine: Yes, exactly. Try it for yourself - rename an image file to foo.txt and open it up in Notepad... you'll see the garbage that's really the image data.Budweis
Okay. Tried, it is exactly as you say. However, it open if i open it with a paint even if it is a txt file.Wittie
@ChitKhine: So that presumably mean paint is ignoring the file extension, which is reasonable. Basically, it's up to each application (and the operating system) to decide how much attention to pay to the extension.Budweis
G
2

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.

Gangrene answered 10/5, 2016 at 6:11 Comment(2)
Does it mean if I write some text into the file without inputting the extension it will be a text file?Wittie
@ChitKhine Of course. It has nothing to do with the name and nothing to do with File.Gangrene
E
-3

if you don't specify a format while creating the file, it becomes a simple collection of bytes with no inherent structure. You'll need to use separate mechanisms to determine the format of the data you write to the file.

Exit answered 3/4 at 6:4 Comment(3)
Specify a format how? Please explain. And new File(...) doesn't create a file in the first place.Gangrene
Maybe you need to refresh your understanding of I/O in Java?Uncommitted
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Tushy

© 2022 - 2024 — McMap. All rights reserved.