skumar
skumar

Reputation: 200

Reading a file in java using fileinputstream

I am new to programming, I need help in understanding the difference between 2 ways of creating a fileinputstream object for reading files. I have seen examples on internet, some have used first one and others second one. I am confused which is better and why?

FileInputStream file = new FileInputStream(new File(path));

FileInputStream file = new FileInputStream(path);

Upvotes: 2

Views: 4181

Answers (5)

monika arora
monika arora

Reputation: 21

There is not much difference between the two , as FileInputStream file = new FileInputStream(path) implicitly calling other.

public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }

But to make better use of two available constructors, we can use constructor taking File argument when there is already a File object so we will be avoiding creation of another file object which will be created implicitly If we are using another constructor

Secondly, It is better to create FileinputStream object only after checking the existence of file which can be checked by using file.exists() in that case we can avoid FileNotFoundException.

Upvotes: 0

Vitaliy
Vitaliy

Reputation: 69

if you want use

FileInputStream file = new FileInputStream(new File(path));

for create FileInputStream need more time, if I don't mistake, because this constructor doing some checks with security manager

Upvotes: 0

moffeltje
moffeltje

Reputation: 4669

The FileInputStream Class has three constructors. Described in the official documentation:

FileInputStream(File file)

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

FileInputStream(String name)

Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

FileInputStream(FileDescriptor fdObj)

Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

As you see here there is no real difference.

Actually they both have the same way to open a file. The first constructor calls

SecurityManager.checkRead(File.getPath())

And the second one uses the same checkRead() as

SecurityManager.checkRead(name)

Upvotes: 3

TheLostMind
TheLostMind

Reputation: 36304

Both are fine. The second one calls the first implicitly.

public FileInputStream(String name) throws FileNotFoundException {
    this(name != null ? new File(name) : null);
}

If you have a reference to the file which should be read, use the former. Else, you should probably use the latter (if you only have the path).

Upvotes: 8

fge
fge

Reputation: 121830

Don't use either in 2015. Use Files.newInputStream() instead. In a try-with-resources statement, at that:

final Path path = Paths.get("path/to/file");

try (
    final InputStream in = Files.newInputStream(path);
) {
    // do stuff with "in"
}

More generally, don't use anything File in new code in 2015 if you can avoid it. JSR 203, aka NIO2, aka java.nio.file, is incomparably better than java.io.File. And it has been there since 2011.

Upvotes: 3

Related Questions