A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/datainputstream-readfully-method-in-java-with-examples/ below:

DataInputStream readFully() method in Java with Examples

DataInputStream readFully() method in Java with Examples

Last Updated : 05 Jun, 2020

The

readFully()

method of

DataInputStream

class in Java is of two types:

  1. readFully(byte[] b) method of DataInputStream class in Java is used to read bytes equal to the length of byte array b from an input stream and store them into the byte array b. General Contract: The readFully(byte[] b) method is blocked in case of one of the following conditions: Syntax:
    public final void readFully(byte[] b)
                      throws IOException
    
    Specified By: This method is specified by the readFully() method of DataInput interface. Parameters: This method accepts one parameter b which represents the byte array into which the data is to be read. Return value: This method does not return any value. Exceptions: Below program illustrates readFully(byte[]) method in DataInputStream class in IO package: Program: Assume the existence of file "c:/demo.txt". Java
    // Java program to illustrate
    // DataInputStream readFully(byte[]) method
    import java.io.*;
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
    
            // Create input stream 'demo.txt'
            // for reading containing
            // text "GEEKSFORGEEKS"
            FileInputStream inputStream
                = new FileInputStream(
                    "c:/demo.txt");
    
            // Convert inputStream to
            // DataInputStream
            DataInputStream dataInputStr
                = new DataInputStream(
                    inputStream);
    
            // Count the total bytes
            int count = dataInputStr.available();
    
            // Create byte array
            byte[] b = new byte[count];
    
            // Read full data into byte array
            dataInputStr.readFully(b);
    
            for (byte by : b) {
                // Print the character
                System.out.print((char)by);
            }
        }
    }
    
    Input: Output:
  2. readFully(byte[] b, int offset, int length) method of DataInputStream class in Java is used to read bytes equal to the parameter passed 'length' from an input stream and store them into the byte array b. General Contract: The readFully(byte[], int, int) method is blocked in case of one of the following conditions: Syntax:
    public final void readFully(byte[] b,
                                int offset,
                                int length)
                         throws IOException
    
    Specified By: This method is specified by the readFully() method of DataInput interface. Parameters: This method accepts three parameters: Return value: This method does not return any value. Exceptions: Below program illustrates readFully(byte[], int, int) method in DataInputStream class in IO package: Program: Assume the existence of file "c:/demo.txt". Java
    // Java program to illustrate
    // DataInputStream readFully(byte[], int, int) method
    import java.io.*;
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
    
            // Create input stream 'demo.txt'
            // for reading containing
            // text "GEEKSFORGEEKS"
            FileInputStream inputStream
                = new FileInputStream(
                    "c:/demo.txt");
    
            // Convert inputStream to
            // DataInputStream
            DataInputStream dataInputStr
                = new DataInputStream(
                    inputStream);
    
            // Count the total bytes
            int count = dataInputStr.available();
    
            // Create byte array
            byte[] b = new byte[count];
    
            // Read full data into byte array
            dataInputStr.readFully(b, 4, 5);
    
            for (byte by : b) {
                // Print the character
                System.out.print((char)by);
            }
        }
    }
    
    Input: Output:
References:

1.

https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readFully(byte%5B%5D)

2.

https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readFully(byte%5B%5D, int, int)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4