Chapter 4 Java.IO (Reading data from keyboard).pdf -...

This preview shows page 1 - 3 out of 13 pages.

Prepared By: Dr. Mahesh ManchandaCSE,GEHUPage 1Chapter 4Standard Input and OutputIntroductionA stream is required to accept input from the keyboard. A stream representsflow of data from one place to another place. It is just like a water-pipe wherewater flows. Like a water-pipe carries water from one place to another, astream carries data from one place to another place. A stream can carry datafrom keyboard to memory or from memory to monitor/printer or from memoryto a file. A stream is always required if we want to move data from one place toanother.Basically, there are two types of streams: input streams and output streams.Input streams are those streams which receive or read data coming from someother place (keyboard/ file/Network socket). Output streams are those streamswhich send or write data to some other place (monitor/printer/file/Networksocket).All streams are represented by classes injava.io(input and output) package.This package contains a lot of classes, all of which can be classified into twocategories: input streams and output streams.Keyboard is represented by a field, called inSystemclass. When we writeSystem.in, we are representing a standard input device, i.e. keyboard, bydefault. System class is found injava.lang(language) package and has threefields. These three streams are created automatically for us as shown below. Allthese fields represent some type of stream:1.System.in:This representsInputStreamobject, which by defaultrepresents standard input device, i.e. keyboard.2.System.out:This representsPrintStreamobject, which by defaultrepresents standard output device, i.e. monitor.3.System.err:This field also representsPrintStreamobject, which by defaultrepresents standard output device, i.e. monitor.Note that bothSystem.outandSystem.errcan be used to represent themonitor and hence any of these two can be used to send data to the monitor.System.outis used todisplay normal messages and results, whereasSystem.erris used to display error messages.Thein,outanderrare static variables ofSystemclass. System class is storedinjava.langpackage which is imported in java program automatically. Static
Prepared By: Dr. Mahesh ManchandaCSE,GEHUPage 2member of any class is always access byclassname.static member, that whyin, out and err is always written as:System.inSystem.out andSystem.errFigure 4.1Reading data from keyboard:There are many ways to read data from the keyboard. For example:InputStreamReaderConsoleScannerCommand Line ArgumentInputStreamReader class:To accept the data from keyboard, i.e.System.in, we need to connect it to aninput stream as some input stream is needed to read data. Figure 4.2 showsthe working detail.Connects the keyboard to an input stream object. Here we can useInputStreamReaderthat can read data from the keyboard.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 13 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
Vivek Singh Rawat
Tags
Object Oriented Programming, Subroutine, Input device, Dr Mahesh Manchanda

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture