Page 328 - computer science (868) class 11
P. 328

11


                                                                       BASIC INPUT/OUTPUT

















                       Learning Objectives


                   11.1  Input/Output in Java                         11.2  The StringTokenizer Class
                   11.3  Introducing Streams                          11.4  Operations on Files




              The data entered through input devices or used in processing is temporarily stored in memory. Once we exit the
              program or switch off our device, this data is lost. It sometimes becomes essential to permanently store input and
              processed data for future reference. This is achieved by using files, which are considered the digital substitute for the
              paper files used to preserve physical documents.

              In this chapter, we will learn about the Scanner class and the Printer class. Next, the concept of the StringTokenizer
              class will be introduced. Then, we will understand how to handle different types of files in Java including binary and
              text files.

                  11.1 INPUT/OUTPUT IN JAVA
              As you know that every program needs an input to process and generate an output. Every programming language
              has specific keywords to accept data from the input device and to display results on the output device. Java provides
              Scanner and PrintStream classes to handle input and output operations respectively. Let us learn about these classes
              in detail.

              11.1.1 The Scanner Class
              This is a revision tour of the Scanner class which is already discussed in Chapter 5 of this book. The Scanner class is used
              to read input from the keyboard for primitive data types including int, float, double, char, etc. It can also read input for
              non-primitive data types like String, File, InputStream(System.in) and similar classes that can execute the interfaces.

              The Scanner class is defined in java.util package. So, while using the Scanner class in a program, we have to import it
              by using the following statement:

                  java.util.*;
                     OR
                  java.util.Scanner;
              Each individual piece of data in the Scanner class is called a token. The tokens are separated by characters called
              delimiters. The default delimiter is whitespace, though any character can be used as a delimiter.




                326326  Touchpad Computer Science-XI
   323   324   325   326   327   328   329   330   331   332   333