What's the difference between using FileInputStream and FileOutputStream or Scanner and PrintStream?

11

Is there a big difference between using these classes?

    
asked by anonymous 01.09.2015 / 02:33

2 answers

10

Java Input and Output

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

Streams

API basics include abstract classes InputStream and OutputStream that define how, respectively, you read and writes sequences to bytes without regard to the source or destination of the data.

Note that these classes provide relatively low-level access because only good bytes work.

File Streams

Extending the base classes, there are several more specific implementations, being FileInputStream and FileOuputStream examples that work specifically with < in> streams of bytes in files.

Buffered streams

Reading or writing byte by 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 < to use the decorator default pattern pattern in> to "decorate" a stream and automatically manage reading and writing in blocks through an internal buffer .

This means that in a read, the class will read BufferedOutputStream bytes at a time to the buffer (being n buffer ) that you actually read one by one in your class. When the buffer is read completely, the class triggers the read of the next block of n bytes.

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

n

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

It also allows you to decorate any FileOutputStream and gives you access to several top-level methods. This pattern is very important to understand this API.

So if you have OutputStream any, such as OuputStream , you can pass that object to the FileOutputStream constructor. When you call a PrintStream method, for example someone who writes a formatted line of text, you are indirectly writing bytes to your original PrintStream , but in a simplified way.

What confuses you a little is that OutputStream also accepts a PrintStream file in your constructor. However, note that this is only a facilitator. If you look at implementation , you will see that it only creates a File .

FileOutputStream

The class Scanner is a different animal. It is not part of the Scanner package.

java.io is a utility class for interpreting simple (primitive) and delimited data into a string of characters. These characters can be read from multiple sources, one of which is sequences of bytes, but can be a simple Scanner .

Do not 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 take full advantage of the language.

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

    
01.09.2015 / 08:10
4

Yes,

  • FileInputStream and FileOutputStream : As the names suggest, they are for dealing with files, the first has methods to read a file and the second has methods to write.
  • Scanner and PrintStream : These are more generic, can be used in other streams (not just files) Scanner is normally used to facilitate console /
Scanner input = new Scanner(System.in);
input.nextLine();
    
01.09.2015 / 03:31