What happens when I open a FileReader / FileWriter?

2

I have a question which basically deals with the classes FileWriter and FileReader . When I installed these classes, I understood that a connection is opened between the chosen file and the java program, now, why do I have to close this connection? What happens internally?

How does BufferedReader work as opposed to FileReader and FileWriter ?

    
asked by MatiEzelQ 21.02.2016 в 03:46
source

3 answers

1

When you create a connection between the file and the program you generate a Stream, these Streams act as a channel between the program and the file, in that channel the required information in bytes is sent. So if you do not close you may still use the resources of your machine that were used for that stream, so when you close close (), you release the resources used for the passage of information between the channel.

FileReader and FileWriter are classes that are usually used to read and write text files when using 16 bits for character flow BufferedReader can improve performance by using the buffer for reading those characters.

Since version 7 of java is no longer used close () instead you can capture the exception automatically with:

try{} and cath(){}

    
answered by 21.02.2016 / 04:36
source
4

BufferedReader and FileReader are classes that extend from the class Reader and serve to implement the design pattern decorator . This pattern is characterized by adding functionality dynamically at runtime. What you have with the class java.io.Reader is a contract that establishes how to read characters from a stream of data (stream). The class FileReader will read characters from a file (whatever its format). The class BufferedReader decorates a java.io.Reader to make it easier to read the characters that exist in that reader when pre-consuming its contents and storing it in a buffer, so as to speed up the reading of the characters in the data flow.

To read the characters of a file, simply do this:

FileReader reader = new FileReader("/ruta/del/archivo.ext");

But the FileReader does not have any additional method to those of Reader , so the direct reading of this class would be character by character or a set of characters stored in an array (denoted by the method FileReader#read(char[], int, int) .

To speed up the performance of the application in these cases, what is usually done is to decorate the instance of FileReader with an instance of BufferedReader , like this:

BufferedReader reader = new BufferedReader(new FileReader("/ruta/del/archivo.ext"));

Now, whenever you work with a physical file, a connection to that file is opened and the application is said to be using the file. By not closing this connection via Closeable#close the operating system will mark that the application continues to use the file, thus generating a problem. Resources must always be closed.

As mentioned by @juliansalas, from Java 7 you can save yourself the call to the close method when using try-with-resources . Here is an example:

try (BufferedReader reader = new BufferedReader(new FileReader("/ruta/del/archivo.ext"))) {
    //consume los datos del reader
    //...
} catch (IOException e) {
    //maneja tus excepciones, SIEMPRE
} /*
    no es necesario agregar un bloque finally para cerrar los recursos
    puesto que esta forma de utilizar try ya cerrará los recursos
    por ti y manejará el caso en que la variable 'reader' no haya
    sido inicializada
*/
    
answered by 21.02.2016 в 04:42
2

To access a file the operating system must provide file descriptors that are , to a certain extent, scarce. What happens when a program opens many files and does not close them is that it exhausts the descriptors that the system provides to its process.

In small programs that open few files it is possible not to realize this, but in more complicated programs or programs that run for a long time (eg, servers), what happens at some point is that they run out and the program is unable to open new files.

  

How does BufferedReader work as opposed to FileReader and FileWriter ?

The BufferedReader is a decorator that adds functionality to a base% Reader . The goal is that the most economical way to read from a file is in blocks, but many programs prefer to read one octet or character at a time. The BufferedReader solves this by reading a block at a time from the Reader base%, saving them temporarily, and serving them little by little to your consumer.

    
answered by 22.02.2016 в 22:25