Java Programming

Stream in Java

 

A stream is a water body that carries water as it flows from one point(source) to another(destination). In programming terminology, a stream can be a continuous group of data or a channel through which data travels from one point(source) to another(destination). An input stream receives data from a source into a program and an output stream sends data to a destination from the program.

Java Stream Example

class BasicIO

{

public static void main(String aa[])

{

byte by[]=new byte[255];

try

{

System.out.println(“Enter a line of text”);

System.in.read(by,0,255);

System.out.println(“The line typed was”);

String stri=new String(by,”Default”);

System.out.println(stri);

}

catch(Exception excep)

{

System.out.println(“Error occurred”);

}

}

}

Stream in Java

Explanation.

The standard input/output stream in java is represented by 3 members of the system class: in, out, and err. These represent the major byte streams provided by java.

System. in—The standard input stream is used for reading characters of data.

System. out—The standard output stream is used to typically display the output on the screen.

System. err—This is the standard error stream.

In this example…

The objective of the code listed in this example is to accept a line from the keyboard and print it on the screen.

A byte array that can hold 255 bytes is created at the beginning of the program. The code System.in.read(by,0,255); allows us to read 255 bytes of data from the keyboard into the byte array.

Then we create a String instance based on the byte array and print it on the screen using the following code.

     String str=new String(by,”Default”);

    System.out.println(str);

It is likely that the operation of reading and writing may cause an exception therefore we enclose the code that performs read and write operations within a try-catch block.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment