Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.

Similar presentations


Presentation on theme: "Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java."— Presentation transcript:

1 N.A.Shulver@staffs.ac.uk Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java I/O philosophy Some java.io classes I/O through java.net classes The “bare-metal” Datagram approach

2 N.A.Shulver@staffs.ac.uk Applications Development Data input and output Java file handling can look complex because it gives great flexibility A typical code fragment which opens a file for reading will use multiple layers of functionality Accesses high-level data types Buffers data for efficiency Reads data from a stream Java App Data Source

3 N.A.Shulver@staffs.ac.uk Applications Development Data input and output In the example that follows, a new DataInputStream object is created to read various data types (bytes, strings, doubles)… but it is a “layer” that runs on top of a generic InputStream-type object providing the file access In the example, the DataInputStream is given a BufferedInputStream object to work with, for efficiency

4 N.A.Shulver@staffs.ac.uk Applications Development Example input code Note that the BufferedInputStream just provides buffering! It is a layer on top of a FileInputStream try {fdinSound = new DataInputStream( new BufferedInputStream( new FileInputStream(sFileName) ) ); } catch(FileNotFoundException e) {System.out.println("File not found: " + sFileName); System.exit(0); }

5 N.A.Shulver@staffs.ac.uk Applications Development Java I/O philosophy Java separates out input/output functionality into different classes Java provides the flexibility to move data into or out of files, arrays of chars, objects… For example, System.out is a system-level PrintStream object for output to the console We can pick and choose layers; we can combine layers in many ways

6 N.A.Shulver@staffs.ac.uk Applications Development Some java.io classes File An abstract representation of file and directory pathnames. FileDescriptor Serves as an opaque handle to the underlying machine- specific structure representing an open file, an open socket, or another source or sink of bytes. DataInputStream, DataOutputStream A data input stream lets an application read/write primitive Java data types from an underlying input/output stream in a machine-independent way.

7 N.A.Shulver@staffs.ac.uk Applications Development Some java.io classes BufferedInputStream, BufferedOutputStream A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. FileInputStream, FileOutputStream A FileInputStream obtains input bytes from a file in a file system. A file output stream is an output stream for writing data to a File or to a FileDescriptor. RandomAccessFile Instances of this class support both reading and writing to a random access file.

8 N.A.Shulver@staffs.ac.uk Applications Development The java.net package How do we link Java’s I/O functionality to the networking classes? When we instantiate a Socket or URLConnection, we get associated InputStream and OutputStream connections // create socket – may throw UnknownHostException, IOException Socket mySock = new Socket(sServerName, nPortNum); InputStream mySockInput = mySock.getInputStream(); NB URL objects only support InputStream

9 N.A.Shulver@staffs.ac.uk Applications Development The java.net package Note that the DatagramSocket does not support streaming access – “connectionless” The DatagramSocket class has myDGSock.send(datagramPacketOut); myDGSock.receive(datagramPacketIn); And that’s just about it for the I/O – a very different approach! Datagrams are “bare-metal” objects and the philosophy of the interface reflects this

10 N.A.Shulver@staffs.ac.uk Applications Development Conclusion We have seen: some of Java’s input/output capabilities the flexibility of Java’s layered I/O approach how the “stream” concept applies to files and other things (arrays, string buffers, objects, sockets) “connectionless” communications


Download ppt "Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java."

Similar presentations


Ads by Google