Java Programming LanguageObjectives                In this session, you will learn to:                   Describe Input/Ou...
Java Programming LanguageI/O Fundamentals               A stream can be thought of as a flow of data from a source        ...
Java Programming LanguageI/O Fundamentals (Contd.)                The fundamental stream classes are:                 Stre...
Java Programming LanguageData Within Streams                  Java technology supports two types of streams:              ...
Java Programming LanguageThe InputStream Methods               The three basic read methods are:                int read()...
Java Programming LanguageThe OutputStream Methods               The three basic write methods are:                void wri...
Java Programming LanguageThe Reader Methods               The three basic read methods are:                 int read()    ...
Java Programming LanguageThe Writer Methods                The basic write methods are:                  void write(int c)...
Java Programming LanguageNode Streams               Various types of Character and Byte Stream classes are:               ...
Java Programming LanguageI/O Stream Chaining                  Input Stream Chain:                    Data Source          ...
Java Programming LanguageProcessing Streams                • Processing Streams are Node Streams that use filters in      ...
Java Programming LanguageInputStream               The InputStream Class Hierarchy:    Ver. 1.0                     Sessio...
Java Programming LanguageOutputStream               The OutputStream Class Hierarchy:    Ver. 1.0                    Sessi...
Java Programming LanguageReader Class                The Reader Class Hierarchy:     Ver. 1.0                    Session 1...
Java Programming LanguageWriter Class                The Writer Class Hierarchy:     Ver. 1.0                     Session ...
Java Programming LanguageNetworking               Basics of Networking:                  Computers running on the Internet...
Java Programming LanguageNetworking (Contd.)                Networking with Java Technology:                   Sockets:   ...
Java Programming LanguageNetworking (Contd.)                  To address the connection, include the following:           ...
Java Programming LanguageNetworking (Contd.)                Java Networking Model:                        Server          ...
Java Programming LanguageServerSocket and Socket Classes                Code Snippet for Creating Minimal TCP/IP Server:  ...
Java Programming LanguageServerSocket and Socket Classes (Contd.)                  bw.close(); // Close the connection, bu...
Java Programming LanguageServerSocket and Socket Classes (Contd.)                // Read the input and print it to the scr...
Java Programming LanguageDemonstration               Lets see how to create a TCP based Java client, and use user-        ...
Java Programming LanguageSummary               In this session, you learned that:                – A stream is a flow of d...
Java Programming LanguageSummary (Contd.)               – Three basic read methods of Reader class are:                   ...
Java Programming LanguageSummary (Contd.)               – To setup a network connection the address or name of remote     ...
Upcoming SlideShare
Loading in …5
×

Java session14

865 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
865
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java session14

  1. 1. Java Programming LanguageObjectives In this session, you will learn to: Describe Input/Output fundamentals Construct node and processing streams, and use them appropriately Distinguish readers and writers from streams, and select appropriately between them Develop code to set up the network connection Understand the TCP/IP Protocol Use ServerSocket and Socket classes for implementation of TCP/IP clients and servers Ver. 1.0 Session 14 Slide 1 of 26
  2. 2. Java Programming LanguageI/O Fundamentals A stream can be thought of as a flow of data from a source or to a sink. A source stream initiates the flow of data, also called an input stream. A sink stream terminates the flow of data, also called an output stream. Sources and sinks are both node streams. Types of node streams are files, memory, and pipes between threads or processes. Ver. 1.0 Session 14 Slide 2 of 26
  3. 3. Java Programming LanguageI/O Fundamentals (Contd.) The fundamental stream classes are: Stream Byte Streams Character Streams Source streams InputStream Reader Sink streams OutputStream Writer Ver. 1.0 Session 14 Slide 3 of 26
  4. 4. Java Programming LanguageData Within Streams Java technology supports two types of streams: character byte • Input and output of character data is handled by readers and writers. • Input and output of byte data is handled by input streams and output streams. • Normally, the term stream refers to a byte stream. • The terms reader and writer refer to character streams. Ver. 1.0 Session 14 Slide 4 of 26
  5. 5. Java Programming LanguageThe InputStream Methods The three basic read methods are: int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length) Other methods include: void close() int available() long skip(long n) boolean markSupported() void mark(int readlimit) void reset() Ver. 1.0 Session 14 Slide 5 of 26
  6. 6. Java Programming LanguageThe OutputStream Methods The three basic write methods are: void write(int c) void write(byte[] buffer) void write(byte[] buffer, int offset, int length) Other methods include: void close() void flush() Ver. 1.0 Session 14 Slide 6 of 26
  7. 7. Java Programming LanguageThe Reader Methods The three basic read methods are: int read() int read(char[] cbuf) int read(char[] cbuf, int offset, int length) Other methods include: void close() boolean ready() long skip(long n) boolean markSupported() void mark(int readAheadLimit) void reset() Ver. 1.0 Session 14 Slide 7 of 26
  8. 8. Java Programming LanguageThe Writer Methods The basic write methods are: void write(int c) void write(char[] cbuf) void write(char[] cbuf, int offset, int length) void write(String string) void write(String string, int offset, int length) Other methods include: void close() void flush() Ver. 1.0 Session 14 Slide 8 of 26
  9. 9. Java Programming LanguageNode Streams Various types of Character and Byte Stream classes are: Type Character Streams Byte Streams File FileReader FileInputStream FileWriter FileOutputStream Memory: array CharArrayReader ByteArrayInputStream CharArrayWriter ByteArrayOutputStream Memory: string StringReader N/A StringWriter Pipe PipedReader PipedInputStream PipedWriter PipedOutputStream Ver. 1.0 Session 14 Slide 9 of 26
  10. 10. Java Programming LanguageI/O Stream Chaining Input Stream Chain: Data Source Program FileInputStream BufferedInputStream DataInputStream • Output Stream Chain: Program Data Sink FileOutputStream BufferedOutputStream DataOutputStream Ver. 1.0 Session 14 Slide 10 of 26
  11. 11. Java Programming LanguageProcessing Streams • Processing Streams are Node Streams that use filters in between while transferring the data. • Various types of Processing Streams are: Type Character Streams Byte Streams Buffering BufferedReader BufferedInputStream BufferedWriter BufferedOutputStream Filtering FilterReader FilterInputStream FilterWriter FilterOutputStream Converting between InputStreamReader bytes and character OutputStreamWriter Performing object ObjectInputStream serialization ObjectOutputStream Performing data DataInputStream Conversion DataOutputStream Counting LineNumberReader LineNumberInputStream Ver. 1.0 Session 14 Slide 11 of 26
  12. 12. Java Programming LanguageInputStream The InputStream Class Hierarchy: Ver. 1.0 Session 14 Slide 12 of 26
  13. 13. Java Programming LanguageOutputStream The OutputStream Class Hierarchy: Ver. 1.0 Session 14 Slide 13 of 26
  14. 14. Java Programming LanguageReader Class The Reader Class Hierarchy: Ver. 1.0 Session 14 Slide 14 of 26
  15. 15. Java Programming LanguageWriter Class The Writer Class Hierarchy: Ver. 1.0 Session 14 Slide 15 of 26
  16. 16. Java Programming LanguageNetworking Basics of Networking: Computers running on the Internet communicating to each other using the Transmission Control Protocol (TCP) / Internet Protocol (IP). Client.bar.com 18000 Server. foo.com 3000 Port no. Client.baz.com 18002 Ver. 1.0 Session 14 Slide 16 of 26
  17. 17. Java Programming LanguageNetworking (Contd.) Networking with Java Technology: Sockets: • Sockets hold two streams, an input stream and an output stream. • Each end of the socket has a pair of streams. Setting Up the Connection: Setup of a network connection is similar to a telephone. One end must dial the other end, which must be listening. Ver. 1.0 Session 14 Slide 17 of 26
  18. 18. Java Programming LanguageNetworking (Contd.) To address the connection, include the following: • The address or name of remote machine. • A port number to identify the purpose at the server. – Port numbers range from 0–65535 Ver. 1.0 Session 14 Slide 18 of 26
  19. 19. Java Programming LanguageNetworking (Contd.) Java Networking Model: Server SeverSocket(port#) Register with ServerSocket.accept() this service Client Wait for a Socket(host, port#) Socket() connection (Attempt to connect) OutputStream OutputStream InputStream InputStream Socket.close() Socket.close() Ver. 1.0 Session 14 Slide 19 of 26
  20. 20. Java Programming LanguageServerSocket and Socket Classes Code Snippet for Creating Minimal TCP/IP Server: ServerSocket s = null; s = new ServerSocket(5432); //Register your service on port 5432 while (true) // Run the listen/accept loop forever { Socket s1 = s.accept(); // Wait here and listen for a connection OutputStream s1out = s1.getOutputStream(); // Get output stream associated with the socket BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s1out)); bw.write(“Hello Net World!n”); // Send your string! Ver. 1.0 Session 14 Slide 20 of 26
  21. 21. Java Programming LanguageServerSocket and Socket Classes (Contd.) bw.close(); // Close the connection, but not the server socket s1.close(); } Code Snippet for Creating Minimal TCP/IP Client: // Open your connection to a server, at port 5432 // localhost used here Socket s1 = new Socket("127.0.0.1", 5432); // Get an input stream from the socket InputStream is = s1.getInputStream(); //Decorate it with a "data" input stream DataInputStream dis = new DataInputStream(is); Ver. 1.0 Session 14 Slide 21 of 26
  22. 22. Java Programming LanguageServerSocket and Socket Classes (Contd.) // Read the input and print it to the screen System.out.println(dis.readUTF()); // When done, just close the steam and connection dis.close(); s1.close(); Ver. 1.0 Session 14 Slide 22 of 26
  23. 23. Java Programming LanguageDemonstration Lets see how to create a TCP based Java client, and use user- provided system properties to drive a Java program. Ver. 1.0 Session 14 Slide 23 of 26
  24. 24. Java Programming LanguageSummary In this session, you learned that: – A stream is a flow of data from a source or to a sink. – A source stream are also called an input stream. – A sink stream terminates the flow of data, also called an output stream. – InputStream, OutputStream, Reader, and Writer are fundamental stream classes. – Three basic read methods of InputStream class are: int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length) – Three basic write methods of OutputStream class are: • void write(int c) • void write(byte[] buffer) • void write(byte[] buffer, int offset, int length) Ver. 1.0 Session 14 Slide 24 of 26
  25. 25. Java Programming LanguageSummary (Contd.) – Three basic read methods of Reader class are: int read() int read(char[] cbuf) int read(char[] cbuf, int offset, int length) – The basic write methods of Writer class are: void write(int c) void write(char[] cbuf) void write(char[] cbuf, int offset, int length) void write(String string) void write(String string, int offset, int length) – FileReader and FileWriter are file type character streams. – FileInputStream and FileOutputStream are byte streams classes. – ServerSocket and Socket are main classes to establish the networking in Java Programs. Ver. 1.0 Session 14 Slide 25 of 26
  26. 26. Java Programming LanguageSummary (Contd.) – To setup a network connection the address or name of remote machine and a port number is required. – accept() method of ServerSocket class is used in server program to receive client sockets to establish connection with the server. – close() method of Socket class is required to close the connection. Ver. 1.0 Session 14 Slide 26 of 26

×