SlideShare a Scribd company logo
1 of 43
Chapter 13
I/O, Applets and Other topics
I/O Basics
• Java programs perform I/O through streams.
• A stream is a logical device that either produces or consumes information.
• A stream is linked to a physical device by the Java I/O system.
• All streams behave in the same manner, even if the actual physical devices to
which they are linked differ.
• Thus, the same I/O classes and methods can be applied to any type of device.
Java defines two types of streams: byte and character.
• Byte streams are used for reading or writing binary data.
• Character streams provide a convenient means for handling input and output of
characters.
The Predefined Streams
• As you know, all Java programs automatically import the java.lang package.
• This package defines a class called System, which encapsulates several aspects of
the run-time environment.
• For example, using some of its methods, you can obtain the current time and the
settings of various properties associated with the system.
• System also contains three predefined stream variables: in, out, and err.
• These fields are declared as public, static, and final within System.
• This means that they can be used by any other part of your program and without
reference to a specific System object.
• System.out refers to the standard output stream. By default, this is the console.
System.in refers to standard input, which is the keyboard by default.
• System.err refers to the standard error stream, which also is the console by
default. However, these streams may be redirected to any compatible I/O device.
• System.in is an object of type InputStream; System.out and System.err are
objects of type PrintStream.
• These are byte streams, even though they typically are used to read and write
characters from and to the console
The Byte Stream Classes
• Byte streams are defined by using two class hierarchies. At the top are two
abstract classes: InputStream and OutputStream.
• Each of these abstract classes has several concrete subclasses that handle the
differences between various devices, such as disk files, network connections, and
even memory buffers.
• Remember, to use the stream classes, you must import java.io.
• The abstract classes InputStream and OutputStream define several key methods
that the other stream classes implement.
• Two of the most important are read( ) and write( ), which, respectively, read and
write bytes of data.
• Both methods are declared as abstract inside InputStream and OutputStream.
• They are overridden by derived stream classes.
The Character Stream
• Classes Character streams are defined by using two class hierarchies. At the top
are two abstract classes, Reader and Writer.
• These abstract classes handle Unicode character streams. Java has several
concrete subclasses of each of these.
• The abstract classes Reader and Writer define several key methods that the other
stream classes implement.
• Two of the most important methods are read( ) and write( ), which read and write
characters of data, respectively.
• These methods are overridden by derived stream classes.
• The BufferedReader class of Java is used to read the stream of characters from
the specified source.
• The constructor of this class accepts an InputStream object as a parameter.
• BufferedReader(Reader inputReader)
• InputStreamReader(InputStream inputStream)
• Instantiate an InputStreamReader (subclass of Reader) class bypassing your
InputStream object as a parameter.
• Because System.in refers to an object of type InputStream, it can be used for
inputstream
• Then, create a BufferedReader, bypassing the above obtained InputStreamReader
object as a parameter.
• This class provides a method named read() and readLine() which reads and
returns the character and next line from the source (respectively) and returns
them.
• The following line of code creates a BufferedReader that is connected to the
keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
• After this statement executes, br is a character-based stream that is linked to the
console through System.in.
• To read a character from a BufferedReader , we use read() method.
• Each time that read( ) is called, it reads a character from the input stream and
returns it as an integer value.
• It returns –1 when the end of the stream is encountered.
• The above program allows reading any number of characters and stores them in
buffer.
• Then, all the characters are read from the buffer till the ‘q’ is found and are
displayed.
• In Java, the data read from the console are treated as strings (or sequence of
characters).
• So, if we need to read numeric data, we need to parse the string to respective
numeric type and use them later in the program
Writing Console Output
• Console output can be achieved using print() and println() methods.
• The PrintStream (which is the type of object referenced by System.out) class provides another
method write() which is capable of printing only low-order 8-bit values.
PrintWriter
• Class PrintWriter is one of the character-based classes.
• System.out is used to write stream of bytes.
• As there is a limitation for size of bytes, for most generic program (that supports
various languages in the world), it is better to use PrintWriter class object to
display the output.
• PrintWriter(OutputStream outputStream, boolean flushOnNewline)
• We can decide whether to flush the stream from the buffer after every newline
by setting 2nd argument of the PrintWriter class constructor as true.
• Here, outputStream is an object of type OutputStream, and flushOnNewline
controls whether Java flushes the output stream every time a println( ) method is
called.
• If flushOnNewline is true, flushing automatically takes place. If false, flushing is
not automatic.
Reading and Writing Files
• Java provides a number of classes and methods that allow you to read and write
files.
• In Java, all files are byte-oriented, and Java provides methods to read and write
bytes from and to a file.
• However, Java allows you to wrap a byte-oriented file stream within a character-
based object.
• Two classes used:
FileInputStream
FileOutputStream
• To open a file, you simply create an object of one of these classes, specifying the
name of the file as an argument to the constructor.
• Two constructors are of the form:
FileInputStream(String fileName) throws FileNotFoundException
FileOutputStream(String fileName) throws FileNotFoundException
• Here, fileName specifies the name of the file that you want to open.
• When you create an input stream, if the file does not exist, then
FileNotFoundException is thrown.
• For output streams, if the file cannot be created, then FileNotFoundException is
thrown.
• When an output file is opened, any preexisting file by the same name is
destroyed.
• When you are done with a file, you should close it by calling close( ).
• To read from a file, you can use a version of read( ) that is defined within
FileInputStream. To write data into a file, you can use the write( ) method defined
by FileOutputStream.
Program to write data into a File:
• To read the data from a file, it is obvious that the file must already exist in
readable format.
• But, to write a data into a file, the file need not exist in the folder.
• Instead, when the statement to open a file to write the data is encountered in the
program a file with specified name will be created.
• The data written will be then stored into it. If the specified file already exists
inside the folder, it will be overwritten by the new data.
• Hence, the programmer must be careful.
Applet Fundamentals
• Java programs
• console-based applications
• applet
• Applets are small applications that are accessed on an Internet server,
transported over the Internet, automatically installed, and run as part of a web
document.
• After an applet arrives on the client, it has limited access to resources so that it
can produce a graphical user interface and run complex computations without
introducing the risk of viruses or breaching data integrity
• The first import statement imports the Abstract Window Toolkit (AWT) classes.
• Applets interact with the user (either directly or indirectly) through the AWT, not
through the console-based I/O classes.
• The AWT contains support for a window-based, graphical user interface.
• The second import statement imports the applet package, which contains the
class Applet.
• Every applet that you create must be a subclass of Applet. The next line in the
program declares the class SimpleApplet.
• This class must be declared as public, because it will be accessed by code that is
outside the program.
• Inside SimpleApplet, paint( ) is declared.
• This method is defined by the AWT and must be overridden by the applet.
• paint( ) is called each time that the applet must redisplay its output. This situation
can occur for several reasons.
• For example, the window in which the applet is running can be overwritten by
another window and then uncovered.
• Or, the applet window can be minimized and then restored. paint( ) is also called
when the applet begins execution.
• Whatever the cause, whenever the applet must redraw its output, paint( ) is
called.
• The paint( ) method has one parameter of type Graphics.
• This parameter contains the graphics context, which describes the graphics
environment in which the applet is running.
• This context is used whenever output to the applet is required.
• Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
• This method outputs a string beginning at the specified X,Y location. It has the
following general form:
void drawString(String message, int x, int y)
• Here, message is the string to be output beginning at x,y.
• In a Java window, the upper-left corner is location 0,0.
• The call to drawString( ) in the applet causes the message “A Simple Applet” to be
displayed beginning at location 20,20
• Notice that the applet does not have a main( ) method.
• Unlike Java programs, applets do not begin execution at main( ).
• In fact, most applets don’t even have a main( ) method.
• Instead, an applet begins execution when the name of its class is passed to an
applet viewer or to a network browser.
• After you enter the source code for SimpleApplet, compile in the same way that
you have been compiling programs.
• There are two ways in which you can run an applet:
• Executing the applet within a Java-compatible web browser.
• Using an applet viewer, such as the standard tool, appletviewer.
• An applet viewer executes your applet in a window. This is generally the fastest
and easiest way to test your applet.
• To execute an applet in a web browser, you need to write a short HTML text file
that contains a tag that loads the applet. Currently, Sun recommends using the
APPLET tag for this purpose
• The width and height statements specify the dimensions of the display area used
by the applet.
• After you create this file, you can execute your browser and then load this file,
which causes SimpleApplet to be executed.
• To execute SimpleApplet with an applet viewer,
• However, a more convenient method exists that you can use to speed up testing.
• Simply include a comment at the head of your Java source code file that contains
the APPLET tag.
• By doing so, your code is documented with a prototype of the necessary HTML
statements, and you can test your compiled applet just by starting the applet
viewer with your Java source code file.
• With this approach, you can quickly iterate through applet development by using
these three steps:
1. Edit a Java source file.
2. Compile your program
3. Execute the applet viewer, specifying the name of your applet’s source file. The
applet viewer will encounter the APPLET tag within the comment and execute
your applet.
• Applets do not need a main( ) method.
• Applets must be run under an applet viewer or a Java-compatible browser.
• User I/O is not accomplished with Java’s stream I/O classes.
• Instead, applets use the interface provided by the AWT or Swing.
Other Topics
Using instanceof
• Sometimes, we need to check type of the
object during runtime of the program.
• We may create multiple classes and objects
to these classes in a program.
• In such situations, the instanceof operator is
useful
• The instanceof operator will return Boolean
value – true or false.
Static Import
• The statement static import expands the capabilities of the import keyword.
• By following import with the keyword static, an import statement can be used to
import the static members of a class or interface.
• When using static import, it is possible to refer to static members directly by their
names, without having to qualify them with the name of their class.
• This simplifies and shortens the syntax required to use a static member.
• We have observed earlier that when we need to use some Math functions, we
need to use Math.sqrt(), Math.pow() etc.
• Using static import feature, we can just use sqrt(), pow() etc.
Invoking Overloaded Constructors through this()
• When working with overloaded constructors, it is sometimes useful for one
constructor to invoke another.
• In Java, this is accomplished by using another form of the this keyword. The
general form is shown here
• When this() is executed, the overloaded constructor that matches the parameter
list specified by arg-list is executed first.
• Then, if there are any statements inside the original constructor, they are
executed.
• The call to this() must be the first statement within the constructor.
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx

More Related Content

Similar to Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx

Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2Gera Paulos
 
7 streams and error handling in java
7 streams and error handling in java7 streams and error handling in java
7 streams and error handling in javaJyoti Verma
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesSakkaravarthiS1
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Outputraksharao
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scannerArif Ullah
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6Berk Soysal
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Java SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).pptJava SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).pptAayush Chimaniya
 
Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Hardeep Kaur
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in javasharma230399
 

Similar to Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx (20)

Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
 
7 streams and error handling in java
7 streams and error handling in java7 streams and error handling in java
7 streams and error handling in java
 
Basic IO
Basic IOBasic IO
Basic IO
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Output
 
Java I/O
Java I/OJava I/O
Java I/O
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
Io streams
Io streamsIo streams
Io streams
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Java
JavaJava
Java
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
Java SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).pptJava SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).ppt
 
Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02Learnadvancedjavaprogramming 131217055604-phpapp02
Learnadvancedjavaprogramming 131217055604-phpapp02
 
Input & output
Input & outputInput & output
Input & output
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 

Recently uploaded

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 

Recently uploaded (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 

Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx

  • 1. Chapter 13 I/O, Applets and Other topics
  • 2. I/O Basics • Java programs perform I/O through streams. • A stream is a logical device that either produces or consumes information. • A stream is linked to a physical device by the Java I/O system. • All streams behave in the same manner, even if the actual physical devices to which they are linked differ. • Thus, the same I/O classes and methods can be applied to any type of device. Java defines two types of streams: byte and character. • Byte streams are used for reading or writing binary data. • Character streams provide a convenient means for handling input and output of characters.
  • 3. The Predefined Streams • As you know, all Java programs automatically import the java.lang package. • This package defines a class called System, which encapsulates several aspects of the run-time environment. • For example, using some of its methods, you can obtain the current time and the settings of various properties associated with the system. • System also contains three predefined stream variables: in, out, and err. • These fields are declared as public, static, and final within System. • This means that they can be used by any other part of your program and without reference to a specific System object. • System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default.
  • 4. • System.err refers to the standard error stream, which also is the console by default. However, these streams may be redirected to any compatible I/O device. • System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. • These are byte streams, even though they typically are used to read and write characters from and to the console
  • 5. The Byte Stream Classes • Byte streams are defined by using two class hierarchies. At the top are two abstract classes: InputStream and OutputStream. • Each of these abstract classes has several concrete subclasses that handle the differences between various devices, such as disk files, network connections, and even memory buffers. • Remember, to use the stream classes, you must import java.io. • The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. • Two of the most important are read( ) and write( ), which, respectively, read and write bytes of data. • Both methods are declared as abstract inside InputStream and OutputStream. • They are overridden by derived stream classes.
  • 6.
  • 7. The Character Stream • Classes Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. • These abstract classes handle Unicode character streams. Java has several concrete subclasses of each of these. • The abstract classes Reader and Writer define several key methods that the other stream classes implement. • Two of the most important methods are read( ) and write( ), which read and write characters of data, respectively. • These methods are overridden by derived stream classes.
  • 8.
  • 9.
  • 10. • The BufferedReader class of Java is used to read the stream of characters from the specified source. • The constructor of this class accepts an InputStream object as a parameter. • BufferedReader(Reader inputReader) • InputStreamReader(InputStream inputStream) • Instantiate an InputStreamReader (subclass of Reader) class bypassing your InputStream object as a parameter. • Because System.in refers to an object of type InputStream, it can be used for inputstream • Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter. • This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.
  • 11. • The following line of code creates a BufferedReader that is connected to the keyboard: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); • After this statement executes, br is a character-based stream that is linked to the console through System.in. • To read a character from a BufferedReader , we use read() method. • Each time that read( ) is called, it reads a character from the input stream and returns it as an integer value. • It returns –1 when the end of the stream is encountered.
  • 12.
  • 13. • The above program allows reading any number of characters and stores them in buffer. • Then, all the characters are read from the buffer till the ‘q’ is found and are displayed. • In Java, the data read from the console are treated as strings (or sequence of characters). • So, if we need to read numeric data, we need to parse the string to respective numeric type and use them later in the program
  • 14.
  • 15.
  • 16.
  • 17. Writing Console Output • Console output can be achieved using print() and println() methods. • The PrintStream (which is the type of object referenced by System.out) class provides another method write() which is capable of printing only low-order 8-bit values.
  • 18. PrintWriter • Class PrintWriter is one of the character-based classes. • System.out is used to write stream of bytes. • As there is a limitation for size of bytes, for most generic program (that supports various languages in the world), it is better to use PrintWriter class object to display the output. • PrintWriter(OutputStream outputStream, boolean flushOnNewline) • We can decide whether to flush the stream from the buffer after every newline by setting 2nd argument of the PrintWriter class constructor as true. • Here, outputStream is an object of type OutputStream, and flushOnNewline controls whether Java flushes the output stream every time a println( ) method is called. • If flushOnNewline is true, flushing automatically takes place. If false, flushing is not automatic.
  • 19.
  • 20. Reading and Writing Files • Java provides a number of classes and methods that allow you to read and write files. • In Java, all files are byte-oriented, and Java provides methods to read and write bytes from and to a file. • However, Java allows you to wrap a byte-oriented file stream within a character- based object. • Two classes used: FileInputStream FileOutputStream
  • 21. • To open a file, you simply create an object of one of these classes, specifying the name of the file as an argument to the constructor. • Two constructors are of the form: FileInputStream(String fileName) throws FileNotFoundException FileOutputStream(String fileName) throws FileNotFoundException • Here, fileName specifies the name of the file that you want to open. • When you create an input stream, if the file does not exist, then FileNotFoundException is thrown. • For output streams, if the file cannot be created, then FileNotFoundException is thrown. • When an output file is opened, any preexisting file by the same name is destroyed. • When you are done with a file, you should close it by calling close( ). • To read from a file, you can use a version of read( ) that is defined within FileInputStream. To write data into a file, you can use the write( ) method defined by FileOutputStream.
  • 22.
  • 23. Program to write data into a File: • To read the data from a file, it is obvious that the file must already exist in readable format. • But, to write a data into a file, the file need not exist in the folder. • Instead, when the statement to open a file to write the data is encountered in the program a file with specified name will be created. • The data written will be then stored into it. If the specified file already exists inside the folder, it will be overwritten by the new data. • Hence, the programmer must be careful.
  • 24.
  • 25.
  • 26.
  • 27. Applet Fundamentals • Java programs • console-based applications • applet • Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a web document. • After an applet arrives on the client, it has limited access to resources so that it can produce a graphical user interface and run complex computations without introducing the risk of viruses or breaching data integrity
  • 28. • The first import statement imports the Abstract Window Toolkit (AWT) classes. • Applets interact with the user (either directly or indirectly) through the AWT, not through the console-based I/O classes. • The AWT contains support for a window-based, graphical user interface. • The second import statement imports the applet package, which contains the class Applet. • Every applet that you create must be a subclass of Applet. The next line in the program declares the class SimpleApplet. • This class must be declared as public, because it will be accessed by code that is outside the program.
  • 29. • Inside SimpleApplet, paint( ) is declared. • This method is defined by the AWT and must be overridden by the applet. • paint( ) is called each time that the applet must redisplay its output. This situation can occur for several reasons. • For example, the window in which the applet is running can be overwritten by another window and then uncovered. • Or, the applet window can be minimized and then restored. paint( ) is also called when the applet begins execution. • Whatever the cause, whenever the applet must redraw its output, paint( ) is called. • The paint( ) method has one parameter of type Graphics. • This parameter contains the graphics context, which describes the graphics environment in which the applet is running. • This context is used whenever output to the applet is required.
  • 30. • Inside paint( ) is a call to drawString( ), which is a member of the Graphics class. • This method outputs a string beginning at the specified X,Y location. It has the following general form: void drawString(String message, int x, int y) • Here, message is the string to be output beginning at x,y. • In a Java window, the upper-left corner is location 0,0. • The call to drawString( ) in the applet causes the message “A Simple Applet” to be displayed beginning at location 20,20
  • 31. • Notice that the applet does not have a main( ) method. • Unlike Java programs, applets do not begin execution at main( ). • In fact, most applets don’t even have a main( ) method. • Instead, an applet begins execution when the name of its class is passed to an applet viewer or to a network browser. • After you enter the source code for SimpleApplet, compile in the same way that you have been compiling programs.
  • 32. • There are two ways in which you can run an applet: • Executing the applet within a Java-compatible web browser. • Using an applet viewer, such as the standard tool, appletviewer. • An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.
  • 33. • To execute an applet in a web browser, you need to write a short HTML text file that contains a tag that loads the applet. Currently, Sun recommends using the APPLET tag for this purpose • The width and height statements specify the dimensions of the display area used by the applet. • After you create this file, you can execute your browser and then load this file, which causes SimpleApplet to be executed. • To execute SimpleApplet with an applet viewer,
  • 34. • However, a more convenient method exists that you can use to speed up testing. • Simply include a comment at the head of your Java source code file that contains the APPLET tag. • By doing so, your code is documented with a prototype of the necessary HTML statements, and you can test your compiled applet just by starting the applet viewer with your Java source code file.
  • 35. • With this approach, you can quickly iterate through applet development by using these three steps: 1. Edit a Java source file. 2. Compile your program 3. Execute the applet viewer, specifying the name of your applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute your applet.
  • 36.
  • 37. • Applets do not need a main( ) method. • Applets must be run under an applet viewer or a Java-compatible browser. • User I/O is not accomplished with Java’s stream I/O classes. • Instead, applets use the interface provided by the AWT or Swing.
  • 38. Other Topics Using instanceof • Sometimes, we need to check type of the object during runtime of the program. • We may create multiple classes and objects to these classes in a program. • In such situations, the instanceof operator is useful • The instanceof operator will return Boolean value – true or false.
  • 39.
  • 40. Static Import • The statement static import expands the capabilities of the import keyword. • By following import with the keyword static, an import statement can be used to import the static members of a class or interface. • When using static import, it is possible to refer to static members directly by their names, without having to qualify them with the name of their class. • This simplifies and shortens the syntax required to use a static member. • We have observed earlier that when we need to use some Math functions, we need to use Math.sqrt(), Math.pow() etc. • Using static import feature, we can just use sqrt(), pow() etc.
  • 41.
  • 42. Invoking Overloaded Constructors through this() • When working with overloaded constructors, it is sometimes useful for one constructor to invoke another. • In Java, this is accomplished by using another form of the this keyword. The general form is shown here • When this() is executed, the overloaded constructor that matches the parameter list specified by arg-list is executed first. • Then, if there are any statements inside the original constructor, they are executed. • The call to this() must be the first statement within the constructor.