Oct 16, 2014 10:17 AM
(This post was last modified: Oct 31, 2014 09:54 AM by MrOrang.)
![[Image: er62IVW.png]](https://i.imgur.com/er62IVW.png)
This tutorial has been brought to you by the Specialists.
Difficulty: Easy
Foreword
First off all, to those of you waiting for the bytecode tutorials to continue. I will do those in another thread. They are just more work than this one and need more time.
In this tutorial you will learn how streams work and how to use them. We will also be going over examples such as copying files, downloading files and more.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
What are Streams?
A stream is something that can transfer data. These are the basics of streams, there exists many more stream types but all streams rely on the simple definition of transfering data.
Here is a simple picture which shows an example stream:
[img]http://docs.oracle.com/javase/tutorial/figures/essential/io-ins.gif[/img]
Source: Click
You probably already heard about music streaming services like Spotify. These stream/transfer data to you which in this case is music.
When you save a file you are also streaming as it writes something from your program to a file on a disk.
I hope that I explained what streaming is well enough. If you have any questions you can always comment on the thread and I will be glad to help you out.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Input/Output?
There exist two kind of streams; input and output streams. An input stream can only READ and an output stream can only WRITE. You should know the differences because else your program will not work.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Example
I will now give an example on how to download a file using streams. Everything is explained in the code so it should be easy to follow.
Code
package the.specialists.looka.testing;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Our test class
* @author Luca
*
*/
public class Test {
/**
* Code entry point
* @param args
*/
public static void main(String[] args) {
try {
/*
* The link we're going to download
*/
URL downloadUrl = new URL("http://pastebin.com/raw.php?i=xcyMsF8N");
/*
* The file we're going to save it to
*/
File saveFile = new File("thingy.txt");
/*
* If the save file already exists delete it(optional)
*/
if(saveFile.exists())
saveFile.delete();
/*
* Create a new saveFile
*/
saveFile.createNewFile();
/*
* Get the inputstream from the url we're going to download
*/
InputStream downloadInputStream = downloadUrl.openStream();
/*
* Get the outputstream for the file so we can write to it
*/
FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
/*
* Read a single byte
*/
int read = downloadInputStream.read();
/*
* Check if the byte read is equal to -1
* -1 is most oftenly used as indicator of end of stream so we know we can stop reading.
*/
while(read != -1) {
/*
* Write the read byte to the file
*/
fileOutputStream.write(read);
/*
* Read the next byte
*/
read = downloadInputStream.read();
}
/*
* Be sure to close BOTH streams when done.
*/
downloadInputStream.close();
fileOutputStream.close();
System.out.println("Done downloading!");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}This little example will download a text file from pastebin into a file called thingy.txt
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Wait a minute, I've heard about readers and writers too, why not use those?
If you did not know yet, there is also something called readers and writers in Java. An example of this is the FileWriter. This will also allow you to write just like an output stream. However there is one big difference. Readers and writers are based around reading/writing characters such as text files and streams are there to read bytes. You can choose which one to use yourself. I choose readers/writers for text files and streams for handling io things such as copying files. I will not go into readers and writers in this tutorial so we will forget about those now.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
What kind of streams are there?
There are a lot of streams in Java ranging from sending entire Java objects to just sending a single byte. In the java.io package you will be able to find (almost)all streams that Java provides.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Obtaining a Stream
If you for example want to write to a file you first need to get an instance of FileOutputStream.
After you got the instance of FileOutputStream you can write to the file by calling a write method.
However there are different ways to obtain an instance of a stream.
With the example I gave earlier I used the method openStream to obtain an InputStream instance. Most of the time you can just get a stream with the new keyword though like with the FileOutputStream.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Reading & Writing
There are different ways to read and write with streams.
The default read methods are the following:
- read:
This will read the next byte from the input stream.
- read(byte[] b)
This will fill the byte array with read bytes. For example; if I have a file containing some information and want to have 10 bytes read I would create a byte array with the size of 10 and then call this method to fill/read it into the byte array.
- read(byte[] b, int off, int len)
This is the more advanced version of read(byte[] b). Here you can specify where to start reading and how much to read from the input stream. For example if I have a large file and want to read from 500 until 510 I would call the method like this:
Code
inputStream.read(bytes, 500, 10);The first parameter of this method is the array to store it in. The second parameter is where to start reading from the stream(Also know as offset) and the last parameter is the length to read which in this case is 10.
Now you want to write your information to something using the OutputStream. There are 3 default write methods just like InputStream has 3 default read methods. These methods are almost identical. The only difference is that the output stream writes and the input stream reads. So lets check the methods out:
- write(byte[] b)
When you use this method you are writing to something. Now lets take a simple text file as exmaple. If I have a text file that already says "I love" I would only have to write the bytes of " the specialists!".
This method will write everything at the end of the already written bytes.
- write(byte[] b, int off, int len)
This method looks a lot like the read(byte[] b, int off, int len) method. However this method will write the byte array at the given location(the off parameter) and will only write the specified length(the len parameter). For example; if I have a file of information and want to write something at index 23 and want to only write 10 bytes I would do the following:
Code
outputStream.write(bytes, 23, 10);![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Flushing?
You have probably heard about the term flushing. What is it and why do we need it?
Flushing only exists in output streams. When you call a flush method it will write all the bytes that were buffered out. When you don't flush you may have the problem of not all bytes being written. For now you don't need to know anything else about flushing as you probably won't need it. You won't have to call flush a lot though. Only if you really need something written like when another program will be reading a file which your program writes.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Closing Streams
The last method you should call when you are done reading or writing is the close method. This method will close the stream preventing any reading or writing. You probably also know the problem when you try to delete a file and it's saying a program is using it? Well that's a stream being busy with that file then. When that stream is closed you will be able to delete the specific file.
If you have an output stream you might think you also have to call the flush method before calling the close method as some bytes may not be written. This is not the case as the close method in an output stream always calls the flush method.
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Copying
In the example I gave earlier I copy data from an inputstream coming from pastebin to an outputstream which in this case is a file. To copy data from an input stream to an output stream you will have to read a byte then check if it's the indicator for end-of-stream and then write it to the output stream.
Here is another example:
Code
/**
* The streams, we will be copying the input stream over to the output stream.
*/
InputStream input = getYourInputStream();
OutputStream output = getYourOutputStream();
/**
* The current read byte
*/
int read = input.read();
/**
* Check if it's end of stream(-1 is end of stream in almost all cases)
*/
while(read != -1) {
/*
*Write the read byte
*/
output.write(read);
/**
* Read the next byte from the input stream
*/
read = input.read();
/**
* Repeat this until the end of stream is reached.
*/
}![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
AutoClosable
In Java 7 there was a new feature introduced named try-with-resource.
This allows you to skip the closing method. Here is an example that uses try-with-resource:
Code
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}I do not have a lot to explain about this rather than showing a piece of code. If you have questions about this feel free to post on my thread!
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Conclusion
I hope you enjoyed, if you still don't understand something please post it in this thread and don't pm me about it as some people may be experiencing the same problem and can then find the answer in this thread.
This is my first tutorial for The Specialists so please give me feedback on what I could improve on and tell me if you see any mistakes!
Thanks!
![[Image: 6XDSKOv.png]](https://i.imgur.com/6XDSKOv.png)
Challenge
Create a program that can backup a folder.
This thread was reviewed by nJoyMe before posting.


![[Image: TEGE4Zx.png]](https://i.imgur.com/TEGE4Zx.png)
![[Image: DTc14yu.png]](https://i.imgur.com/DTc14yu.png)