Upload Your Textio.java File and Save It Under the Default Package

Java IO : Input-output in Java with Examples

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams back up all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:

  1. System.in: This is the standard input stream that is used to read characters from the keyboard or whatever other standard input device.
  2. System.out: This is the standard output stream that is used to produce the result of a program on an output device like the calculator screen.

    Here is a list of the various impress functions that we use to output statements:

    • print(): This method in Java is used to display a text on the console. This text is passed as the parameter to this method in the grade of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The side by side printing takes identify from just here.
      Syntax:
      Arrangement.out.print(parameter);

      Case:

      import java.io.*;

      course Demo_print {

      public static void master(String[] args)

      {

      System.out.impress( "GfG! " );

      System.out.print( "GfG! " );

      System.out.impress( "GfG! " );

      }

      }

      Output:

      GfG! GfG! GfG!                  
    • println(): This method in Java is also used to display a text on the console. It prints the text on the console and the cursor moves to the commencement of the side by side line at the console. The next printing takes place from the next line.
      Syntax:
      System.out.println(parameter);

      Example:

      import java.io.*;

      class Demo_print {

      public static void main(String[] args)

      {

      Arrangement.out.println( "GfG! " );

      Organisation.out.println( "GfG! " );

      Organisation.out.println( "GfG! " );

      }

      }

      Output:

      GfG!  GfG!  GfG!                  
    • printf(): This is the easiest of all methods as this is similar to printf in C. Annotation that Organisation.out.print() and Organisation.out.println() take a single argument, simply printf() may take multiple arguments. This is used to format the output in Java.
      Example:

      class JavaFormatter1 {

      public static void main(Cord args[])

      {

      int x = 100 ;

      System.out.printf(

      "Printing simple"

      + " integer: x = %d\north" ,

      ten);

      Arrangement.out.printf(

      "Formatted with"

      + " precision: PI = %.2f\n" ,

      Math.PI);

      float n = v .2f;

      Arrangement.out.printf(

      "Formatted to "

      + "specific width: north = %.4f\n" ,

      n);

      n = 2324435 .3f;

      System.out.printf(

      "Formatted to "

      + "right margin: n = %20.4f\n" ,

      northward);

      }

      }

      Output:

      Printing simple integer: x = 100 Formatted with precision: PI = 3.xiv Formatted to specific width: n = five.2000 Formatted to right margin: n =         2324435.2500
  3. System.err: This is the standard error stream that is used to output all the error data that a program might throw, on a reckoner screen or whatsoever standard output device.

    This stream also uses all the three above-mentioned functions to output the error information:

    • print()
    • println()
    • printf()

    Example:

    import java.io.*;

    public class SimpleIO {

    public static void main(String args[])

    throws IOException

    {

    InputStreamReader inp = nix ;

    inp = new InputStreamReader(System.in);

    System.out.println( "Enter characters, "

    + " and '0' to quit." );

    char c;

    do {

    c = ( char )inp.read();

    System.out.println(c);

    } while (c != '0' );

    }

    }

    Input:

    GeeksforGeeks0

    Output:

    Enter characters, and '0' to quit. Thousand e e k s f o r G e e k s 0

Types of Streams:

  • Depending on the type of operations, streams tin be divided into two master classes:
    1. Input Stream: These streams are used to read data that must be taken as an input from a source assortment or file or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
    2. Output Stream: These streams are used to write information as outputs into an assortment or file or any output peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.
  • Depending on the types of file, Streams can be divided into two main classes which tin be further divided into other classes as tin can be seen through the diagram beneath followed by the explanations.
    1. ByteStream: This is used to process information byte past byte (8 bits). Though it has many classes, the FileInputStream and the FileOutputStream are the most popular ones. The FileInputStream is used to read from the source and FileOutputStream is used to write to the destination. Here is the list of various ByteStream Classes:
      Stream grade Description
      BufferedInputStream It is used for Buffered Input Stream.
      DataInputStream Information technology contains method for reading java standard datatypes.
      FileInputStream This is used to reads from a file
      InputStream This is an abstract form that describes stream input.
      PrintStream This contains the virtually used print() and println() method
      BufferedOutputStream This is used for Buffered Output Stream.
      DataOutputStream This contains method for writing java standard information types.
      FileOutputStream This is used to write to a file.
      OutputStream This is an abstract form that describe stream output.

      Instance:

      import java.io.*;

      public form BStream {

      public static void main(

      String[] args) throws IOException

      {

      FileInputStream sourceStream = zero ;

      FileOutputStream targetStream = nil ;

      endeavour {

      sourceStream

      = new FileInputStream( "sorcefile.txt" );

      targetStream

      = new FileOutputStream( "targetfile.txt" );

      int temp;

      while ((

      temp = sourceStream.read())

      != - 1 )

      targetStream.write(( byte )temp);

      }

      finally {

      if (sourceStream != zippo )

      sourceStream.shut();

      if (targetStream != null )

      targetStream.close();

      }

      }

      }

      Output:

      Shows contents of file exam.txt                  
    2. CharacterStream: In Java, characters are stored using Unicode conventions (Refer this for details). Character stream automatically allows usa to read/write data character by graphic symbol. Though it has many classes, the FileReader and the FileWriter are the most popular ones. FileReader and FileWriter are graphic symbol streams used to read from the source and write to the destination respectively. Hither is the list of diverse CharacterStream Classes:
      Stream grade Clarification
      BufferedReader Information technology is used to handle buffered input stream.
      FileReader This is an input stream that reads from file.
      InputStreamReader This input stream is used to translate byte to graphic symbol.
      OutputStreamReader This output stream is used to translate character to byte.
      Reader This is an abstract grade that define grapheme stream input.
      PrintWriter This contains the most used print() and println() method
      Author This is an abstract class that ascertain character stream output.
      BufferedWriter This is used to handle buffered output stream.
      FileWriter This is used to output stream that writes to file.

      Example:

      import java.io.*;

      public form GfG {

      public static void primary(

      String[] args) throws IOException

      {

      FileReader sourceStream = cypher ;

      effort {

      sourceStream

      = new FileReader( "test.txt" );

      int temp;

      while ((

      temp = sourceStream.read())

      != - 1 )

      Organisation.out.println(( char )temp);

      }

      finally {

      if (sourceStream != null )

      sourceStream.close();

      }

      }

      }

      Refer here for the complete divergence between Byte and Character Stream Class in Coffee.


hernandezfartandence.blogspot.com

Source: https://www.geeksforgeeks.org/java-io-input-output-in-java-with-examples/

0 Response to "Upload Your Textio.java File and Save It Under the Default Package"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel