4

I've been showed to read file using the following pattern:

File path = new File("src/modul/lorem.txt");
FileReader fr = new FileReader(path);    
// ... BufferedReader br = new BufferedReader(fr); ... and so on ...

As far as I know file is a class which represents more a path to class then an actual file: Java Docs Now, when I tried using just a String instead of the File-object ...

 // ...
 FileReader fr = new FileReader("src/modul/lorem.txt");
 // ...

... it worked fine too.

Therefore my question:

What's the purpose of making and using a File-object?

Is there a real benefit? Or is it just a pattern one has seen somewhere and then copied without asking?

cluster1
  • 4,968
  • 6
  • 32
  • 49
  • AFAIK the first API you described accepts a `File` perhaps just as a matter of convenience. If someone already have a `File` object in hand, the first API is easy to use. As you described, a `File` in Java just wraps a path, rather than the actual content. – Tim Biegeleisen May 27 '17 at 08:16
  • 2
    Your use-case seems to be limited to reading from a file. There are a lot of other things, like checking if the file is executable, finding if its a directory, listing its children etc. File conveniently provides methods for many tasks. Did you read the entire doc? – ThreeSidedCoin May 27 '17 at 08:17
  • 2
    There is no difference in this case. Sometimes you have a `File`, sotimes a `String`. It's entirely up to you which is more convenient. – user207421 May 27 '17 at 08:18
  • @ThreeSidedCoin Yep. I know a bit about the methods you mention. I've seen how it is used for checking if a resource is a directory. But exactly that made me wonder if it isn't often just used for being conform with some pattern. Because I have seen code-examples in which none of the File-methods are used. – cluster1 May 27 '17 at 08:22
  • Read [this](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/FileInputStream.java#FileInputStream). Your FileReader(String) constructs a file object from the String. – ThreeSidedCoin May 27 '17 at 08:23
  • @ThreeSidedCoin Thanks. :) – cluster1 May 27 '17 at 08:26
  • Thanks for the quick accept! And for the record : I enhanced my answer a little bit. Hope you find that helpful. – GhostCat May 27 '17 at 08:29
  • @GhostCat I have to thank. :) – cluster1 May 27 '17 at 08:32
  • I, personally, consider one point important: `System.out.println("DEBUG: Why the XXXX does it not find "+file.getAbsolutePath()+"???");` - you know what I mean.... – Marco13 May 27 '17 at 08:50

4 Answers4

3

This simply depends on your context/requirements.

Java's File class is nothing but an abstraction. It allows you to treat "some thing" knowing that it represents a file system object. Quoting the javadoc:

An abstract representation of file and directory pathnames.

So, in general: if you only need to pass a file name when opening a reader, then a flat string does fine. But as soon as it might be helpful to do other things with that file, then using the abstraction is the way to go.

In other words: keep in mind that a string is just a sequence of characters. It doesn't bear any meaning beyond that. There is no "meta data" there. But as Java is a strictly typed language, we simply have distinct types for things that "mean more" than just "a sequence of characters".

Example: the file class provides methods to check if the file really exists. When using flat strings, you have to wait for the reader to throw an exception at you.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Am I right that `File` is never preferable in new projects? https://stackoverflow.com/a/26658436/4592248 – iwis Jul 15 '23 at 20:33
  • I think that is a fair statement. Of course, there might be situations when you need File objects, for example when dealing with some older library ... but even then you probably prefer a Path object, and turn that into a File when needed. – GhostCat Jul 17 '23 at 06:02
1

File is a class that contains a lot of operations and information about a file and directories. FileReader, on the other hand, is a class that allows you to read a file without complicating your existence so much.

There's not a real benefit, because you're asking for two different things.

1

One purpose would be, as you have already said (https://docs.oracle.com/javase/7/docs/api/java/io/File.html), that you can benefit from methods that are additional and more specifically than those from String.

So it depends on your goal, if you would better use Collection<File> or Collection<String> for example.

Of course you can convert a File object to a String representation, and vice versa.
But it matters for design, performance, etc...

For the constructor invocation of FileReader, it makes no real difference, but more for the surrounding architecture.

ratech
  • 11
  • 5
1

In this case, there is no difference. You could use string or file as you wish as FileReader has both constructors.

Below is the javadocs of FileReader class.

/** * Creates a new FileReader, given the name of the * file to read from. * * @param fileName the name of the file to read from * @exception FileNotFoundException if the named file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */

public FileReader(String fileName) throws FileNotFoundException {
    super(new FileInputStream(fileName));
}

/** * Creates a new FileReader, given the File * to read from. * * @param file the File to read from * @exception FileNotFoundException if the file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */

public FileReader(File file) throws FileNotFoundException {
    super(new FileInputStream(file));
}

So, it accepts both File object and String object as a parameter.

harshavmb
  • 3,404
  • 3
  • 21
  • 55