I have the following code that allows me to read the content of a file (in this case txt) by console in Java. I don't quite understand what the function of FileInputStream, InputStreamReader and BufferedReader is. What exactly do they do? Thank you!
public class LeerFichero {
public static void main(String[] args) {
int numero = 0;
try {
Scanner sc = new Scanner(System.in);
System.out.println("Introduce el nombre del fichero: ");
String nombreFichero = sc.nextLine();
String salida = "C:\\Users\\SAG\\Desktop\\Repaso\\src\\leerarchivo\\";
FileInputStream fichero = new FileInputStream(salida + nombreFichero + ".txt");
InputStreamReader osw = new InputStreamReader(fichero);
BufferedReader br = new BufferedReader(osw);
while(br.ready()) {
String linea = br.readLine();
System.out.println(linea);
}
}catch(FileNotFoundException e) {
e.getMessage();
}catch(IOException e) {
e.getMessage();
}
}
}
Flow of input and output of data in the file system
.I/O Streams
For any flow of input or output data, objects called streams are used, which transport the data between the program and any other external agent, for example a hardware device, the input of commands from the console, the communication with other programs or the reading and writing files.
Java divides the stream architecture into two large groups:
Within these two groups there is a structure of classes capable of formatting data, for example to read a byte, a character, primitive types or even an object. Buffers are also available to optimize data read/write performance.
. We distinguish the following types of streams:
They are implementations of the classes
java.io.InputStream
andjava.io.OutputStream
.Among these we can find classes like..
. FileInputStream
. FileOutputStream
-. These classes can be instantiated by passing the path to a file in the class constructor. To take into account to catch the possible exceptions. They will normally be derived from
java.io.IOException
.. Once the stream is opened, it will proceed to read or write sequentially until the end of the file.
Binary data input and output:
The most used implementations of this type of streams are
FileReader
andFileWriter
:-.
Un InputStreamReader
is a bridge between byte streams and character streams: it reads the bytes and decodes them into characters using a specific character set.-. For greater efficiency, consider wrapping a
InputStreamReader
within aBufferedReader
.For example:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Alphanumeric data input and output:
BufferedReader:
Reads text from an input stream of characters, buffering the characters to provide efficient reading of those characters, arrays, and lines. In a clearer way, it allows us to read text from an InputStream in a simple way.FileInputStream is used to read, it is practically the same as with FileReader , using the read() method , when it reaches the end of the file it returns -1. Their basic difference is that with FileReader we read characters and FileInputStream reads bytes . For example:
For java, a Reader class is a class that reads characters. This is more like what we want. A Reader has methods for reading characters. With this class we could already work. The pity is that we still have System.in , which is an InputStream and not a Reader .
How do we convert the System.in to Reader ? There is a class in java, the InputStreamReader , that does this conversion for us. To get a Reader , we just have to instantiate an InputStreamReader by passing an InputStream in the constructor . The code is the following
We are declaring a variable "isr" of type InputStreamReader . We create an object of this class by doing new InputStreamReader(...) . In parentheses we pass the InputStream that we want to convert to Reader , in this case, the System.in
We already have the Reader . How exactly does it work?
InputStreamReader is a Reader . It behaves the same as in Reader and can be put on any site that supports a Reader . That is, we can read characters from it. When building it we have passed an InputStream , specifically, System.in. InputStreamReader somehow stores it inside. When we ask InputStreamReader for characters, it asks the InputStream that has the bytes stored inside, converts them to characters and returns them to us.
The mechanism to obtain a BufferedReader from any other Reader (for example the InputStreamReader ), is similar to the one we used before. We instantiate it by passing the Reader to it in the constructor . The code is:
The behavior of this class is the same as the InputStreamReader . When we ask for a complete line of characters (a String ), it asks the Reader inside it, converts it to a String and returns it to us.
To ask for a String , the readLine() method is used . This method reads all typed characters (received if it were another input device) until it finds the keystroke,or whatever you want to call it.
This reads a complete String from the keyboard and stores it in a "text" variable.
I hope this helps you: https://www.discoduroderoer.es/clases-fileinputstream-y-fileoutputstream-para-ficheros-binarios-en-java/
http://www.javamexico.org/foros/comunidad/outputstream_y_inputstream_que_means_and_how_work with this I hope it is clear to you
They are mainly used for file manipulation.