File Input/Output Basic Java Tutorial - Get an output to a file using java.io.PrintWriter

preview_player
Показать описание

In this video, we are going to see how to send the output of a java program to a file stored on your local computer. This way of doing becomes useful when we want to get the output of a program in a printed format.

Now, let me show you how this works, with the help of an example

The String variable firstname is used to store the first name
The variable name is going to store the name and the method next() is used to get the input value form the source that is Myfile
The variable age will store the age
And the variable height is used to store the height

We are using these predefined methods to get the input values from the file

Now, as I said at the beginning, in order to store the output of a program in a file, you have to use the class PrintWriter
I will start by declaring a PrintWriter variable and associate this variable with the destination that is the file where the output will be stored

Always make sure that you are writing the correct path to the file and using two backward slashes because of the escape character sequence rules

After that, you can now use the predefined methods print or println with the output file
Once the output statement is done, we have to close the file using the close method
This will ensure that the buffer holding the output will be emptied and the entire output generated by the program will be sent to the output file

If I run the program now, the value inside the brackets will be stored in the output file OutputFile
Open the output file, and see that we have My name is : John Doe
The statement assumes that the values stored in the variable firstname and name are John and Doe
That’s exactly what we specified in the statements above

We can do the same thing for the other variables

So guys, that is how to send output of your java program to file

Thanks for viewing, I hope this video was informative and please do not forget to like and Subscribe to this channel for more. Let’s meet in the next one.

#codingriver
#java
#programming
Рекомендации по теме
visit shbcf.ru