Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.

Similar presentations


Presentation on theme: "Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard."— Presentation transcript:

1 Java IO

2 Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard drive, the Web, and the rest of the Internet. I/O gives your Java program access to your hard drive, the Web, and the rest of the Internet.

3 Stream A stream is a path of communication between a source of information and a destination. A stream is a path of communication between a source of information and a destination. If we’re the source, the stream is an output stream. If we’re the source, the stream is an output stream. If we’re the destination, the stream is an input stream. If we’re the destination, the stream is an input stream.

4 Standard Streams Java provides. Java provides. System.In-KB. System.Out-monitor. System.Err-monitor. It is possible to redirect a program’s input stream so that it comes from a file. It is possible to redirect a program’s input stream so that it comes from a file. Having a separate error stream allows programs to display error messages even when the standard output stream has been redirected. Having a separate error stream allows programs to display error messages even when the standard output stream has been redirected.

5 Streams Vs Readers/writers Generalized communication mechanisms.  Streams are byte oriented.  Readers\Writers are character (Unicode) oriented.

6 FileInputStream & FileOutputStream Two of the most useful things you can do with data is send and receive it to and from the hard drive. Two of the most useful things you can do with data is send and receive it to and from the hard drive. FileOutputStream writes data into files. FileInputStream reads data from files. FileOutputStream writes data into files. FileInputStream reads data from files. FileOutputStream can create a new file and write the data into it. FileOutputStream can create a new file and write the data into it.

7 ByteArrayOutputStream & ByteArrayInputStream ByteArrayOutputStream : To store your array of bytes into your computer’s local memory. ByteArrayOutputStream : To store your array of bytes into your computer’s local memory. ByteArrayInputStream : To retrieve an array of bytes from your computer’s local memory. ByteArrayInputStream : To retrieve an array of bytes from your computer’s local memory. Used for complex entities. Used for complex entities.

8 Buffered Reads & Buffered Writes BufferedInputStream—Used for reading large amounts of data, by buffering input for efficiency. BufferedInputStream—Used for reading large amounts of data, by buffering input for efficiency. BufferedOutputStream—Used for writing out data in large blocks, using buffers for efficiency. BufferedOutputStream—Used for writing out data in large blocks, using buffers for efficiency. They buffer input/output so bytes of data can be read/written from/to devices in larger groups. They buffer input/output so bytes of data can be read/written from/to devices in larger groups. The flush() method causes any buffered data to be immediately written to the output stream. The flush() method causes any buffered data to be immediately written to the output stream.

9 Character IO If you need to output characters, use a Writer rather than an OutputStream. If you need to output characters, use a Writer rather than an OutputStream. Readers work better with 16-bit chars, and are designed for Unicode. Readers work better with 16-bit chars, and are designed for Unicode. Use Readers and Writers to Support Internationalization. We should write Unicode, rather than ASCII. Use Readers and Writers to Support Internationalization. We should write Unicode, rather than ASCII.

10 Reading Lines Character based streams give you the flexibility by allowing you to read lines of text terminated by carriage return. Character based streams give you the flexibility by allowing you to read lines of text terminated by carriage return. readLine( ) method of BufferedReader class provides this capability. readLine( ) method of BufferedReader class provides this capability.

11 Display Formatting PrintWriter class is the character -stream class corresponding to the PrintStream byte-stream class. PrintWriter class is the character -stream class corresponding to the PrintStream byte-stream class. This should be used to generate the textual output for users. This should be used to generate the textual output for users. print( ) and println( ) methods of PrintWriter class accept primitive values and objects. print( ) and println( ) methods of PrintWriter class accept primitive values and objects. flush( ) method suspends processing until the output is delivered. flush( ) method suspends processing until the output is delivered. You can use automatic flushing specifying true in the constructor. You can use automatic flushing specifying true in the constructor.

12 Platform Independent Data Formatting A collection of methods of DataInputStream and DataOutputStream classes supports platform independent data formatting. A collection of methods of DataInputStream and DataOutputStream classes supports platform independent data formatting. These methods translate the internal coding of java’s primitive types into sequence of bytes that can be read and written. These methods translate the internal coding of java’s primitive types into sequence of bytes that can be read and written.

13 Methods: Boolean readBoolean( ) Boolean readBoolean( ) Byte readByte( ) Byte readByte( ) Char readChar( ) Char readChar( ) Float readFloat( ) Float readFloat( ) int ReadInt( ) int ReadInt( ) String readLine( ) String readLine( ) Long readLong( ) Long readLong( ) Short readShort( ) Short readShort( ) String readUTF( ) -read a unicode string String readUTF( ) -read a unicode string Void writeBoolean(boolean v) Void writeBoolean(boolean v)

14 …and corresponding write methods in the DataOutputStream class. Subclasses of InputStream may be wrapped within a DataInputStrean. Subclasses of InputStream may be wrapped within a DataInputStrean. In fact, it is possible to wrap BufferedInput stream. In fact, it is possible to wrap BufferedInput stream.

15 File Object File object provides a number of methods to manipulate files and directories. File object provides a number of methods to manipulate files and directories. File can refer either a directory or a file, using a relative or absolute path. File can refer either a directory or a file, using a relative or absolute path. Constructors: Constructors: File(string path). File(string path, string filename). File(file path, string filename). If path is not null, filename interpreted as relative path.

16 Methods: Methods: Boolean canRead( ) Boolean canWrite( ) Boolean delete( ) Boolean equals(object obj) Boolean exists( ) String getAbsolutePath( ) String getName( ) String getParent( ) String getPath( ) String getPath( )

17 Boolean isAbsolute( ) Boolean isDirectory( ) Boolean isDirectory( ) Boolean isFile( ) Boolean isFile( ) Long lastModified( ) Long length( ) String[ ] list( ) Boolean mkdir( ) Boolean mkdirs( ) Boolean mkdirs( ) Boolean renameTo(file dest)

18 Fields: Fields: String pathSeparator String pathSeparator Char pathSeparatorChar String separator Char separatorChar

19 RandomAccessFiles RandomAccessFiles class can be used for both input and output to a single file. RandomAccessFiles class can be used for both input and output to a single file. It provides similar platform independent formatting methods as the DataInputStream class. It provides similar platform independent formatting methods as the DataInputStream class. RandomAccessFile theFile = new RandomAccessFile(“Test.text”, “rw”); RandomAccessFile theFile = new RandomAccessFile(“Test.text”, “rw”);

20 The constructor has two arguments: The constructor has two arguments: 1. The first argument specifies the file. 2. The second specifies as ‘r’ read only, ‘w’ write only or ‘rw’ both. If the specified file does not exists, it is automatically created. If the specified file does not exists, it is automatically created. It is possible to direct a RandomAccessFile to read or write beginning at any position within a file, by the use of a seek( ) method. It is possible to direct a RandomAccessFile to read or write beginning at any position within a file, by the use of a seek( ) method. You specify the byte-offset from the beginning of file with a long. You specify the byte-offset from the beginning of file with a long.

21 File Dialogs File dialog class makes it possible to write programs platform independent, while still using operating system specific dialogs. File dialog class makes it possible to write programs platform independent, while still using operating system specific dialogs. File dialog class helps make it easy to build programs that let the user navigate the file system and open or saves files. File dialog class helps make it easy to build programs that let the user navigate the file system and open or saves files.

22 Constructors: Constructors: FileDialog(Frame parent, String title) Defaults to FileDialog.LOAD for mode FileDialog(Frame parent, String title, int mode) FileDialog(Frame parent, String title, int mode) First argument frame that owns the dialog Second argument specifies the title Third argument specifies the mode that the dialog uses.LOAD and SAVE

23 Java Networking

24 Layers of Network

25 Networking The simplest way to create a network aware java program is to use the URL class. The simplest way to create a network aware java program is to use the URL class. URL allows you to explicitly locate and name any document existing anywhere on the www. URL allows you to explicitly locate and name any document existing anywhere on the www.

26 URL theConnection = new URL(“http://www.JavaSoft.Com”); URL theConnection = new URL(“http://www.JavaSoft.Com”); Once you have created an URL class, a plethora of methods to get the content of the document and to perform other useful operations. Once you have created an URL class, a plethora of methods to get the content of the document and to perform other useful operations. Methods. Methods. Boolean equals(Object obj). Boolean equals(Object obj).

27 Object getContent( ) Object getContent( ) String getFile( ) String getFile( ) String getHost( ) String getHost( ) String getPort( ) String getPort( ) String getProtocol( ) String getProtocol( ) String getRef( ) String getRef( ) URLConnection openConnection( ) URLConnection openConnection( ) InputStream openStream( ) InputStream openStream( ) Boolean sameFile(URL doc) Boolean sameFile(URL doc)

28 URLConnection URLConnection class allows you to get information about documents that are referenced by URL objects. URLConnection class allows you to get information about documents that are referenced by URL objects. Use openConnection( ) method of URL class to get the URLConnection object. Use openConnection( ) method of URL class to get the URLConnection object.

29 Methods: Void connect( ) Void connect( ) Object getContent( ) Object getContent( ) String getContentEncoding( ) String getContentEncoding( ) int getContentLength( ) int getContentLength( ) String getContentType( ) String getContentType( ) Long getExpiration( ) Long getExpiration( )

30 URL getURL(). URL getURL(). InputStream getInputStream(). InputStream getInputStream(). OutputStream getOutputStream(). OutputStream getOutputStream(). Void setDoInput(boolean value). Void setDoInput(boolean value). Void setDoOutput(boolean value). Void setDoOutput(boolean value).

31 Sockets Sockets are streams of data that can flow between networked computers. Sockets are streams of data that can flow between networked computers. Java has facility to work directly with sockets. Java has facility to work directly with sockets. Socket has two forms of identification: Socket has two forms of identification: Host and port. Host and port. This allows each host to have a number of sockets in operation, distinguished by its port number. This allows each host to have a number of sockets in operation, distinguished by its port number.

32 One called server and the other called client. One called server and the other called client. Client requests data and the server supplies it. Client requests data and the server supplies it.

33 ServerSockets ServerSocket class is used to create a server application that listens on a specified port for a client requesting service. ServerSocket class is used to create a server application that listens on a specified port for a client requesting service. To create a ServerSocket object, you use a constructor that takes port number as an integer argument. To create a ServerSocket object, you use a constructor that takes port number as an integer argument. Accept( ) method can be used to obtain socket object for private conversation between the server and the client. Accept( ) method can be used to obtain socket object for private conversation between the server and the client.

34 Methods: T accept( ). Void close( ). Void close( ). InetAddress getInetAddress( ) - get the IP address of the client. InetAddress getInetAddress( ) - get the IP address of the client. int getLocalPort( ) - return the local port on which the ServerSocket is listening. int getLocalPort( ) - return the local port on which the ServerSocket is listening.

35 InetAddress Class Every machine connected to internet has a unique address, called IP address. Every machine connected to internet has a unique address, called IP address. InetAddress object is retrieved by use of the ServerSocket class’s getInetAddress( ) method. InetAddress object is retrieved by use of the ServerSocket class’s getInetAddress( ) method. InetAddress class provides several class methods that allow you to retrieve an InetAddress object. InetAddress class provides several class methods that allow you to retrieve an InetAddress object.

36 Class methods: InetAddress[ ] getAllByName(string host) InetAddress[ ] getAllByName(string host) InetAddress getByName(string host) InetAddress getByName(string host) InetAddress getLocalHost( ) InetAddress getLocalHost( ) Methods: Methods: Byte[ ] getAddress( ) Byte[ ] getAddress( ) String getHostName( ) String getHostName( )

37 Socket Class The Socket class is used by the server and the client to converse. The Socket class is used by the server and the client to converse. The Socket object is returned by the accept() method of the ServerSocket class. The Socket object is returned by the accept() method of the ServerSocket class. Constructors can also be used to create the Socket class. Constructors can also be used to create the Socket class. Socket(String host, int port). Socket(String host, int port). Socket(InetAddress ipNumber, int port). Socket(InetAddress ipNumber, int port).

38 Methods: Void close( ). Void close( ). InetAddress getInetAddress( ). InetAddress getInetAddress( ). InputStream getInputStream( ). InputStream getInputStream( ). int getLocalPort( ). int getLocalPort( ). OutputStream getOutputStream( ). OutputStream getOutputStream( ). int getPort( ). int getPort( ).…….……………….End…………...………...

39 Methods: Methods: String getDirectory( ) String getFile( ) int getMode( ) Void setDirectory(string dir) Void setFile(string file) Void setFilenameFilter(FilenameFilter flt)


Download ppt "Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard."

Similar presentations


Ads by Google