Page 353 - Cs_withBlue_J_C11_Flipbook
P. 353

12                  BASIC INPUT/OUTPUT AND



                                                                     DATA FILE HANDLING
















                         Learning Objectives


                     12.1  Input/Output in Java                          12.2  The StringTokenizer Class
                     12.3  Data File Handling                            12.4  Introducing Streams
                     12.5  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.

                     12.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.

                 12.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.



                                                                                                                       351
                                                                                 Basic Input/Output and Data File Handling    351
   348   349   350   351   352   353   354   355   356   357   358