Java Basics

Java Basics 14: Reading / writing files

14 videos

80 minutes

For the last topic we'll see in this basic course is the writing and reading files with Java, so let's do something very simple, which has many utilities for power, stored in a text file, some information we want, and how to recover it, this is used quite in programming. So, let's see how. The first is that to utilize the functionality we need, to read and write, we need to

import from a library that is what allows us to use these functions, so we will import from: 'java.io'. and here we could select the one that we would like to keep things simple we will select them all, and it is true we are importing more than the necessary libraries, and this is not a very good practice to not make it more complicated, with more data, and there we can use the functions, then here we will see first, how to write,

since there may be errors in writing, either by: permission, or anything that comes not complete, we will put it inside the 'try catch' and here in this way we can control the errors so that the program does not block or not paralyzed, but know how to handle that error, if it occurs in an extension, an error, we will call this extension, 'ex' because here we will say that the display so that the user knows, what

has passed, is only that, but we have done nothing here read simply, we have kept back, in case something happens. All right! Now if we write ... we create a variable, type: 'FileWrite' this variable exists in these files that we have imported, we will call the 'file' and here we go to say that this is a new, 'FileWrite' and we will tell you how be called the file in which we will write, so we'll put:

Jotajotavm.txt Perfect! And this file is to be created, where will create this file? ... In their own folder, the user where I am working document / NetBeansProyect, and the name of the project you've been told, there is where you will create the file. This is the file we are I created, and he there simply write a message, so I'll put, fichero.write, and here we write the message, for example so we do a,

holaaaaaaa, or could pass as a parameter variable type String, which we found previously created, and we said, the value it will have, if I I throw it, and write the file, and close ... this is very important, whenever they open a file, ciérrenlo! Do not forget! ... We close, we launched, and says it is running properly, let's see if it's true! ... And here I created this file, and if I open it there puts the message that I have

said with java, you have to create, check! We know how to write .... Now let's see how to read! I copied all this again, as if he had also failed reading, and now here instead of reading, rather than create me one, FileWrite, I'll create a 'FileReader' and this will be a FileReader, and I have to tell which file is what I want you to read, good! Now if this if it changes, then what we will do is ... that this file, not to give the same name

before, I'll call and here other reader, I have to create another variable that is of type BufferedReader, which is going to have associated the functions, reading, going to give, 'BR' to make it easier and this will make a new 'BufferedReader,' ok and yes, what will be associated is the reader that we just created before, so they know which file is read !, what logically have to know, where to read, and now here we will

show on screen what you have read, 'System.out.println'. Here and we will show on screen what you have read is BR which is the name of the variable, we to call the function: 'readLine' to read a line, ok! Let's throw it, and I have to show on the screen: 'holaaaaaaa' Exactly!

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.