11
Is there any big difference between using these classes?
11
Is there any big difference between using these classes?
10
To better understand how these classes work you should understand a little about the package’s Java I/O API java.io
.
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.
Extending the basic classes, there are different more specific implementations, being FileInputStream
and FileOuputStream
examples working specifically with streams bytes in files.
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
.
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,
Scanner input = new Scanner(System.in);
input.nextLine();
Browser other questions tagged java input-output
You are not signed in. Login or sign up in order to post.