Introduction
- This is going to be my first post on streams in java and we are going to start with byte streams. A quick disclaimer about byte streams, you are probably never going to use them because there are usually streams better suited for the situation. However, with that being said, all other streams are based off of byte streams. So it is helpful to know and understand them. I have also created a Youtube version of this post. So please make sure to check that out as well.
I/O Streams
Before we get into byte streams lets first get a solid understanding about what streams are. A stream is a generic term that represents data moving from one point to another. A stream can represent many different kinds of sources and destinations, like disk files, devices, other programs and even memory arrays.
Some streams simply pass data while others manipulate and transform the data in useful ways. There are two main kind of streams,
1) InputStreams: a program uses an input stream to read data from a destination
1) OutputStreams: a program uses an output stream to write data to a destination one item at a time
Byte Streams
- Programs use byte streams to perform input and output of 8-bit bytes. A byte simply being what we call 8 bits packaged together. Now we will get into the creating of our Byte Stream, I have created a YouTube video of the code and I would recommend that you watch that video and do a code along. You will get much more out of the video than this text.
public class CopyBytes {
public static void main(String[] args) throws IOException{
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("anotherOne.txt");
out = new FileOutputStream("itDoBeLikeThat.txt");
int c;
while((c = in.read())!= -1) {
out.write(c);
}
//close the streams
}finally {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
}
}
}
Understanding the code
1) main this method is called "public static void main(String[] args)", this is a special method in java. Every application in java must have this method as it acts as the starting point for the application.
2) throws this is a mechanism for handle exceptions in Java. If you are unfamiliar with exception handling in Java I would highly recommend that you give that a quick google. Essentially throws allows us to handle the IOException exception without having to fully implement try/catch
3) FileInputStream is for obtaining input bytes from a file in the file system.
4) FileOutputStream is for writing data to a file
5) try is another part of exception handling in java. The try{} is called a try block. Code that has a chance of throwing an exception should always be placed inside of a try block.
-
6) new FileInputStream("anotherOne.txt") this creates a new FileInputStream from the file called "anotherOne.txt", please make sure that you have already pre created this folder inside of your root directory. If you fail to do this then an error will occur.
- 7) new FileOutStream("itDoBeLikeThat.txt") this is us creating and output stream, which is going to be a new file called "itDoBeLikeThat.txt" in the root directory and this is where all the text from "anotherOne.txt" is going to be placed.
8) int c; this is where we created a simple integer and call it c. This will be used for looping and writing data to the desired file.
9) while((c = in.read())!= -1) {out.write(c);} this is how we loop through "anotherOne.txt"s data. The read() method will return a number between 0 - 255 on each interaction and at the end of the data it will return -1. read() returning -1 is how we know that there is no more data left to read.
10) finally this is another part of java exception handling. For the purpose of this post all we need to know is that it will run no matter what. Even if an exception does occur, this block of code will still execute. When closing streams, put the code responsible for closing the streams inside the finally block.
11) .close() this is responsible for closing the streams and it is very important that we do so. Closing streams helps us avoid serious resource leaks.
Top comments (0)