What’s the difference between using Fileinputstream and Fileoutputstream or Scanner and Printstream?

Asked

Viewed 3,004 times

11

Is there any big difference between using these classes?

2 answers

10

Java Input and Output

To better understand how these classes work you should understand a little about the package’s Java I/O API java.io.

Streams

The basics of the API include abstract classes InputStream and OutputStream which define how, respectively, you read and write byte sequences regardless of the source or destination of the data.

Note that these classes establish a relatively low-level access as they only work well.

File Streams

Extending the basic classes, there are different more specific implementations, being FileInputStream and FileOuputStream examples working specifically with streams bytes in files.

Buffered streams

Read or write byte to byte is not efficient, after all most disks and memories are optimized to work with data blocks.

For this, there are classes like BufferedInputStream and BufferedOutputStream using the project standard Decorator to "decorate" a stream and automatically manage reading and writing in blocks through a buffer intern.

This means that, in a reading, the class will read n bytes at once for the buffer (being n the size of buffer) even if you effectively read one by one in your class. When the buffer is read through, class triggers reading of the next block of n bytes.

The same goes for recording. Even if you record one byte at a time, the implementation bufferized will only trigger the recording on disk after you have enough characters to fill the buffer.

PrintStream

The class PrintStream is also a subclass of OutputStream, as well as FileOutputStream.

It also allows you to decorate any OutputStream and gives you access to several higher-level methods. This standard is very important to understand this API.

So if you have one OuputStream any, such as a FileOutputStream, you can pass this object in the PrintStream. When you call a method of PrintStream, for example some writing a formatted line of text, you will be indirectly recording bytes in your OutputStream original, but more simplified.

What’s a little confusing is that PrintStream also accepts a file File in its builder. However, note that this is only a facilitator. If you look at the implementation, will see that he just creates a FileOutputStream.

Scanner

Already the class Scanner is a different animal. It is not part of the package java.io.

Scanner is a utility class for interpreting simple (primitive) data and delimited in a string. These characters can be read from several sources, one of them being strings of bytes, but can be a simple String.

Don’t stop here

There are many other classes in the Java Input and Output API. Each of them has a purpose and it is important to know some of them to make the most of the language.

Also, for even more performance-consistent implementations, consider the NIO (New Input/Output) API, which is the newest and recommended form of file access in Java. See the official documentation here.

4

Yes,

  • Fileinputstream and Fileoutputstream: as the names suggest, are to handle files, the first has methods to read a file and the second has methods to write.
  • Scanner and Printstream: are more generic, can be used on other streams(not just files), Scanner is usually used for easy input of console/terminal data:
Scanner input = new Scanner(System.in);
input.nextLine();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.