![]() |
|
[Java] [Specialists] Java Streams Tutorial - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] [Specialists] Java Streams Tutorial (/showthread.php?tid=4490391) Pages: Pages
1
2
|
[Specialists] Java Streams Tutorial - MrOrang - Oct 16, 2014 ![]() 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. ![]() 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. ![]() 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. ![]() 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;This little example will download a text file from pastebin into a file called thingy.txt ![]() 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. ![]() 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. ![]() 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. ![]() 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);![]() 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. ![]() 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. ![]() 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 /**![]() 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 {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! ![]() 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! ![]() Challenge Create a program that can backup a folder. This thread was reviewed by nJoyMe before posting. * - Psycho_Coder - Oct 16, 2014 Nice tutorial. It seems " Specialists ", is a different and experienced group. I like being member of a programming group, but i have not the post - requirements at this moment. Btw, thank you for the contribution, Psycho_Coder. * - nOrr - Oct 16, 2014 Nip-picking mode activated - You shouldn't use JavaDoc comments for single line comments. Use // or at the very most multi-line comments where it makes sense ( /* comment / and NOT /* Javadoc */) - If you share catch logic (here, all you do is print the stack trace) you should use try with multi-catch for easier reading. This is a Java 7 feature if I remember right. - Why do you prefer streams over readers/writes when working with text files? - You should mention the AutoCloasble interface ( http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html ) imo. Fix these points and you're a step closer to calling yourself a specialist in entry level streams. :P * - MrOrang - Oct 16, 2014 (Oct 16, 2014 10:32 AM)nOrr Wrote: *Nip-picking mode activated* I'll improve on it when I get back on my pc. Maybe I'll add a piece about AutoCloseable. Oh and I choose readers/writers for text files and streams for handling io things such as copying files. * - nOrr - Oct 16, 2014 (Oct 16, 2014 10:54 AM)๖ۣۜLooka Wrote: I'll improve on it when I get back on my pc. Maybe I'll add a piece about AutoCloseable. Oh and I choose readers/writers for text files and streams for handling io things such as copying files. Mention that in the post then. ;) Currently you have: Quote:You can choose which one to use yourself. I prefer to use streams most of the time though. I will not go into readers and writers in this tutorial so we will forget about those now. * - ObscureCoder - Oct 16, 2014 Nice thread, will be helpful for beginners. Spoiler (Click to View) * - nOrr - Oct 16, 2014 (Oct 16, 2014 01:00 PM)Bibl Wrote: Uhm... Yaaaarhh? 🤨 (Oct 16, 2014 02:21 PM)ObscureCoder Wrote: OT: If you guys want a real challenge, actually try and do a JNI thread. JNI is a f*cking sour c*nt. Is it? Why? I haven't actually worked with JNI, but what's "c*nty" about it? * - MrOrang - Oct 16, 2014 (Oct 16, 2014 02:21 PM)ObscureCoder Wrote: Nice thread, will be helpful for beginners.I probably wont do JNI as I dont have any knowledge about the C language. Maybe someone else could do a tutorial about it if they want? * - ObscureCoder - Oct 16, 2014 (Oct 16, 2014 02:24 PM)nOrr Wrote: Is it? Why? I haven't actually worked with JNI, but what's "c*nty" about it? Well, for a starter, since it's C++ you have to compile dlls and so files for both 32 bit and 64 bit which in my experience has been very frustrating with the compilers and IDEs I use. It's a real pain in the ass. A lot of people in this section talk about JNI but have never used it and if they had actually used it they'd probably stop talking about how 'great' it is for things like RATs etc. since it's a total nightmare in reality. That's why. If you have the time and patience for it then go for it but it's a nightmare. I'm currently trying to compile a 64 bit dll using g++ for JNI so it will actually load in the 64bit JRE 1.8. I've had to turn to stackoverflow for help. * - Bibl - Oct 16, 2014 urm jni is only a problem is you're terrible at c/c++ otherwise its perfectly fine * - ObscureCoder - Oct 16, 2014 (Oct 16, 2014 03:33 PM)Bibl Wrote: urm jni is only a problem is you're terrible at c/c++ otherwise its perfectly fine Obviously. JNI takes a lot of work for 32 bit and 64 bit cross platform use. (Oct 16, 2014 03:35 PM)ObscureCoder Wrote: Obviously. I take this back actually. JNI is a f*cking doddle if you install the correct f*cking C++ compiler xD I was compiling for the wrong JVM arch. * - nOrr - Oct 17, 2014 (Oct 16, 2014 02:54 PM)ObscureCoder Wrote: Well, for a starter, since it's C++ you have to compile dlls and so files for both 32 bit and 64 bit which in my experience has been very frustrating with the compilers and IDEs I use. It's a real pain in the ass. A lot of people in this section talk about JNI but have never used it and if they had actually used it they'd probably stop talking about how 'great' it is for things like RATs etc. since it's a total nightmare in reality. You say this as if SO is a place like HF. :D It sounds like a fun-but-frustrating project indeed. Surely it must be possible and surely there must exists tools to help you. If it's a common problem, there will be common solutions. Edit: (Oct 16, 2014 03:35 PM)ObscureCoder Wrote: I take this back actually. See? Told you!! 😝 🤣 * - ObscureCoder - Oct 17, 2014 (Oct 17, 2014 08:53 AM)nOrr Wrote: You say this as if SO is a place like HF. :D It kind of is though. You can post malware related questions and people will answer and not even care if it's for malicious purposes or not. * - Matt AH - Oct 17, 2014 Awesome tutorial by a great programmer. Hope this can be used by everyone. * - MrOrang - Oct 17, 2014 (Oct 17, 2014 10:03 AM)Matt AH Wrote: Awesome tutorial by a great programmer. Hope this can be used by everyone. Thanks man. I will be sure to continue tutorials like this. whoring for bronze award I'll also add a piece about AutoCloseable maybe later tonight or tomorrow. |