Java Stream, File, and IO

Posted by tranzparency on Sun, 13 Feb 2022 19:18:14 +0100

Java Stream, File, and IO

Java Stream, File, and IO

  • Java. The IO package contains almost all the classes needed to operate on inputs and outputs
    • All these stream class stream classes represent input sources and output destinations
  • Java. Streams in io packages support many formats
    • Examples include basic types, objects, localized character sets, and so on
  • A stream can be understood as a sequence of data
    • Input stream represents reading data from a source
    • Output stream represents writing data to a target
  • Java provides strong and flexible support for I/o, making it more widely used in file transfer and network programming

Read Console Input

  • Java console input is made by System.in complete

    • In order to get a character stream bound to the console, you can use System.in wraps in a BufferedReader object to create a character stream

      • Basic syntax for creating a BufferedReader:

        BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));
        
        • After the BufferedReader object is created, you can use the read() method to read a character from the console or the readLine() method to read a string

Read multicharacter input from console

  • To read a character from a BufferedReader object, use the read() method

    • Syntax: int read throws IOException

      • Each time the read() method is called, a character is read from the input stream and returned as an integer value

      • Return-1 when the stream ends

      • This method throws IOException

    • Example of using BufferedReader to read characters in the console

      //Use BufferedReader to read characters in the console
      
      import java.io.*;
      public class BRRead{
      public  static void main(String[] args){
      char c;
      //Use System.in Create BufferedReader
      BudderedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter characters, press q Key Exit");
      do{
      c=(char) br.read();
      System.out.println(c);
      }while (c!='q');
      }
      }
      

Read string from console

  • Reading a string from standard input requires the reaLine() method of BufferedReader

    • Syntax: String readLine() throws IOException

      Use BufferedReader Examples of reading characters in the console
      //Use BufferedReader to read characters in the console
      impor  java.io.*;
      public class BRReadLines{
      public static void main(String[] args)  throws IOException{
      //Use System.in Create BufferedReader
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in);
      String str;
      System.out.println("Enter lines of text.");
      System.out.println("Enter 'end' to quit.");
      do{
      Str = br.readLine();
      System.out.println(str);
      }while(!str.equals("end"));                                                                                                                                         
      }
      }     
      

console output

  • The output of the console is done by print() and println()

    • These methods are defined by the class PrintStream
    • System.out is a reference to this object
  • PrintStream inherits the OutputStream class and implements the method write(); Write() can also be used to write operations to the console

    • PrintStream defines the simplest format for write():

      • Void write(int byteval)

        • This method writes the low octet bytes of byteval to the stream

        • Example: Use write() to output the character "A" and the following line breaks to the screen:

          import  java.io.*;
          //Demonstrate System.out.write()
          public class WriteDemo{
          public static void main(String [] args){
          int b;
          b = 'A';
          System.out.write(b);
          System.out.write('\n');
          }
          }
          

Read and write files

  • A stream is defined as a data sequence
    • Input stream is used to read data from source
    • The output stream is used to write data to the target
  • Two important streams, FilelnputStream and FileOutputStream

FilelnputStream

  • The stream is used to read data from a file, and objects of FilelnputStream can be created with the new keyword

  • There are several ways to create objects

    • You can use string-type filenames to create an input stream object to read files

      InputStream  f =new FileInputStream("C:/java/hello");
      
    • You can also use a file object to create an input stream object to read files

    • First you have to create a file object using the File() method

      File  f  = new File("C:/java/hello");
      InputStream  in = new FileInputStream(f); 
      
  • public void close() throws IOException{}

    • Close this file input stream and release all system resources associated with it
    • Throw IOException Exception
  • protected void finalize() throws IOException{}

    • Method Clears the connection to the file
    • Make sure to call the close method of the file input stream when it is no longer referenced
    • Throw IOException Exception
  • public int read(int r) throws IOException{}

    • This method reads the specified byte of data from the InputStream object
    • Return to integer value
    • Returns the next byte of data, or -1 if it has reached the end
  • public int read (byte[] r) throws IOException{}

    • This method reads r.length length bytes from the input stream
    • Returns the number of bytes read
    • Returns -1 if it is at the end of the file
  • Public int available() throws IOException{}

    • Returns the number of bytes that the next method called on this input stream can read from this input stream without blocking
    • Returns an integer value

FileOutputStream

Topics: Java Back-end