2

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);

5 Answers 5

8

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Darn you beat me by a second :)
@Kayaman - Not my mistake.. Thread.sleep(1000) didn't work :( :P
3

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.

Comments

3

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)

Comments

0

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

Comments

0

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.

1 Comment

your answer is almost similar to @TheLostMind answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.