Hello in this Java programming video, we are going to look at File Stream and we're going to more specifically look at the byte stream. So we're going to load the file in, we're going to save to a file as well, we're just going to be using a simple byte stream, we'll look at more advanced stuff like standard and catering in a separate video, so feel free to take a look at them. The first of all, we need to have an info. And this is an important java.io io Asterix like so. And what we will also need is to IO exception and we'll need this preventing errors. And what we're going to do over the course of the next few videos, the best done within like a try catch block, that's going to be covered in a separate section in this series.
So once you've done the input output stuff, the file IO section which is what we're doing right now, have a look at the try catch the exception section, they combine them to gather. So it just helps prevent any errors or like handle any errors and prevent it from crashing the application. So provide some default behavior. So when the file input screen input equal No. Okay, so this is what we're going to be using to load our file in and input equals new file inputs three. What we're going to do is specify the format of coil input dot txt, and I've already got default created.
So you want this where your sonar system class file is within your compiled, you know application. So if you go to your workspace your project, so the bin so once we actually run it in the clips. If you compile it using something else, maybe the terminal, this is where the thing would appear. And you want to place your file right here at the moment is empty, we'll add some content to reverse. So once you've done that, we want to be able to, you know, go over the content printed out. So we're going to say int number.
I'm going to say while so we're going to be we, so the byte stream, you read it in every character in our file as a non binary be at the ASCII number for it. So while number equals input don't lead doesn't equal negative one when n equals negative one, we're at the end of the file basically. And we are just going to do a system What we're going to do now is System dot out dot print ln. And in here, we're gonna specify the number. So save that. And if we run it, we get an error.
No such file or directory, okay? That is fine. It doesn't exist, you know, here. But you will when we actually run it by the console. So we do change directory to this right? Why?
No produce Java sonar system. We don't say class is empty because we haven't actually put anything in there. We put in hello world. And I'm actually going to put an A at the end. Just because, you know, we know that uppercase A is the ASCII equivalent of 65. So if I save that, run this again.
Doo doo Okay, what actually happened? It seemed like he was trying to do something. Okay, so let's just you know, go through the code. So we do input new false thing file input dot txt that line number. So while number when we do equal to input dot read is not equal to negative one, the system that I haven't put anything in here, i for i did with so many again, we get all the value and the last value, which is a character uppercase a is 65. That's how you read stuff using the byte stream to white stuff.
It's really simple. So if we do file, output stream and call this output equals no To output we just do output equals new file, stream. And we specify the file on the say, awesome dot txt. And now we just do x dot right. So what we're going to write and put Sorry, I put I will write 65, for example. So if we were to run this let's actually run this again, the fall being created, because the it did not you know, hit the error on the file input stream.
So we printed all this and we go to awesome dot txt, even though we put it devoid the ASCII equivalent is uppercase A. So that's it for file input and output using the byte stream. Next I will cover standard stream, we'll cover case spring, we'll cover all the really cool stuff, add an extra task, I want you to try and figure out how you can say print out your name or print your name and write your name to a file. And so let me just give you a little help. So if I were to fix here now, and if I were to run this as you can see a nova dot write method call, we'll just put that particular character at the end of that file. You can obviously use a space character if you want to a tab character to have formatted for free to do that as well but try and print your name not so keep that in print.
You can reuse in print so much. Try putting your name saving it into a text file. So that's it for this video. If you have any questions, feel free to reach out and I look forward to seeing In the next awesome Java video