Is there a big difference between using these classes?
Is there a big difference between using these classes?
To better understand how these classes work you should understand a little about the Java I / O API of the java.io
package.
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.
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.
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
.
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 .
Yes,
Scanner input = new Scanner(System.in);
input.nextLine();