Read a file by console with java

1

I have the following code that allows me to read the contents of a file (in this case txt) by Java console. I do not really understand what the function of FileInputStream, InputStreamReader and BufferedReader is. What exactly do they do? Thanks!

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();
    }

}

}

    
asked by Sergio AG 28.05.2018 в 21:10
source

3 answers

2

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, communication with others programs or the reading and writing of files.

Java divides the architecture of streams into two large groups:

  • Those that work with bytes
  • And those who do it with characters.
  • Within these two groups there is a class structure capable of formatting the 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:

      

    1. Byte Streams: Read or write data in bytes.

    They are implementations of the classes java.io.InputStream and java.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 constructor of the class. To take into account capture possible exceptions. Normally they will 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:

      

    2 . Character Streams: The use is similar to the byte stream, but in this case we will read text files character by character.

    The most commonly used implementations of this type of streams are FileReader and FileWriter :

    -. Un InputStreamReader is a bridge between the sequences of bytes and the sequences of characters: it reads the bytes and decodes them in characters using a specific character set.

    -. For greater efficiency, we must consider wrapping a InputStreamReader within a BufferedReader .

    For example: BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    Alphanumeric data entry and exit:

      

    3 . Buffered Streams: We will use this type of streams accompanied by other streams, creating buffers to optimize the performance of the program.

    BufferedReader: Reads text from a character input sequence, 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.

        
    answered by 29.05.2018 / 10:35
    source
    0

    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 -one. Its basic difference is that with FileReader we read characters and FileInputStream reads bytes . For example:

    import java.io.*;
    public class FicherosBinariosApp {
    
        public static void main(String[] args) {
    
            try(FileInputStream fis=new FileInputStream("D:\fichero_bin.ddr")){
    
                int valor=fis.read();
                while(valor!=-1){
                    System.out.print((char)valor);
                    valor=fis.read();
                }
    
            }catch(IOException e){
    
            }
    
        }
    
    }
    

    For java, a class Reader is a class that reads characters. This is more like what we want. A Reader has methods to read characters. With this class we could already work. Sorry, we still have System.in , which is a InputStream and not a Reader .

    How do we convert the System.in to Reader ?. There is a class in java, the InputStreamReader , which makes this conversion. To get a Reader , we just need to instantiate a InputStreamReader by passing a InputStream in the constructor. The code is as follows

    InputStreamReader isr = new InputStreamReader(System.in);
    

    We are declaring a variable "isr" of type InputStreamReader . We create an object of this class by doing new InputStreamReader (...) . In parentheses we passed the InputStream that we want to convert to Reader , in this case, the System.in

    We already have the Reader . How does it work exactly?

    InputStreamReader is a Reader . It behaves the same as in Reader and can be put anywhere that supports a Reader . That is, we can read characters from it. When building it we have passed a InputStream , specifically, System.in. InputStreamReader somehow it is stored inside. When we ask for InputStreamReader characters, it asks the InputStream which has the bytes saved, converts them to characters and returns them to us.

    The mechanism to obtain a BufferedReader from another Reader , anyone (for example the InputStreamReader ), is similar to the one we used before. We instantiate it by passing it on the Reader . The code is:

    BufferedReader br = new BufferedReader (isr);
    

    The operation of this class is the same as the InputStreamReader . When we ask for a complete line of characters (a String ), she asks the Reader that she has inside, converts them to String and returns it to us.

    To request a String , the readLine () method is used. This method reads all typed characters (received if it was another input device) until you find the key press , or whatever you want to call it.

    String texto = br.readLine();
    

    This reads a full String from the keyboard and stores it in a "text" variable.

        
    answered by 29.05.2018 в 11:54
    -1

    I hope this works for you: link

    link with this I hope you are clear

    mainly used for file manipulation

        
    answered by 28.05.2018 в 22:05