CS3006N File input and Output Programming Section 7 - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CS3006N File input and Output Programming Section 7

Description:

By default all attributes in the class are sent across the network this means ... If the versions are not the same an Exception is generated ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 35
Provided by: james488
Category:

less

Transcript and Presenter's Notes

Title: CS3006N File input and Output Programming Section 7


1
CS3006N File input and Output Programming Section
7
  • James King
  • 12 August 2003

2
Topics
  • Object oriented concepts
  • How Java programs work
  • First steps
  • Importing classes
  • Graphical classes
  • Applets
  • Input and Output Streams
  • Multiple threads of execution
  • Network programming

3
Input and Output Streams
  • Before you can program network applications you
    need to learn how to read and write files and
    serialise objects
  • Streams in Java
  • InputStream
  • OutputStream
  • Reader
  • Writers
  • Serialising your own objects

4
Basic Streams, readers and writers
  • Data sources such as files or network connections
    can only be read or written to using streams or
    readers and writers
  • The basic streams provided a data source are
  • InputStream
  • OutputStream
  • The basic readers and writers are
  • Reader
  • writer
  • In themselves they are not very useful because
    they only provide methods to read and write one
    or more bytes.
  • However we can attach more streams/ readers/
    writers to the basic ones to be able to read and
    write plain text, primitive Java types or Objects

5
How Input Streams Work
  • An InputStream reads data sequentially from a
    data source
  • The first read() reads the first byte in the
    file/network connection etc and the next read
    reads the next byte
  • You can skip() a number of bytes
  • You can find out if there are any bytes to read
    using available()
  • Unfortunately it is not easy to go back and read
    a previous byte

6
How Output Streams Work
  • An OutputStream writes data sequentially to a
    data source
  • The first write() writes the first byte in the
    file/network connection etc and the next write
    writes the next byte
  • Unfortunately it is not easy to go back and
    modify a previous byte
  • Writes are buffered in memory and sent when the
    buffer is full. To force data to be written to
    the data source use flush()

7
Specialised Streams
  • Some data sources support more functionality than
    others and require specialised input and output
    streams to work with them.
  • Files FileInputStream FileOutputStream
  • Audio AudioInputStream AudioOutputStream
  • etc..

8
reading and Writing simple Java types
Input and Output Streams Section
9
Reading and Writing More than just bytes
  • We can read and write ints, objects, Strings and
    ASCII/UNicode text etc. by attaching a more
    specialised stream to the general input and
    output streams.
  • DataInputStream and DataOutputStream streams
    allow you to write simple Java types such as int,
    boolean float and Strings
  • PrintStream allows you to write Unicode text
  • Unfortunately there is no longer a way to read
    Unicode text using streams
  • ObjectInputStream and ObjectOutputStream allow
    you to read and write objects in Javas native
    format

10
Connecting Streams Together
  • You connect streams together by getting an
    instance of a stream and giving it to the
    constructor of the next stream
  • FileInputStream fi new FileInputStream(f)
  • DataInputStream isnew DataInputStream(fi)
  • Now the FileInputStream has a DataInputStream
    attached to it to allow us to read simple Java
    data types

11
Stream Example Reading
  • File fnew File("c/blob")
  • DataInputStream isnew DataInputStream(new
    FileInputStream(f))
  • String sis.readUTF()
  • System.out.println(s)

12
Stream Example Writing
  • File fnew File("c/blob")
  • DataOutputStream isnew DataOutputStream(new
    FileOutputStream(f))
  • is.writeUTF("hello world")
  • is.flush()

13
reading and Writing ASCII characters
Input and Output Streams Section
14
Reading and Writing Unicode/ASCII
  • To read and write Unicode and ASCII correctly we
    have to abandon the Stream system and use the
    Reader Writer system
  • There are equivalent buffered readers and writers
    for each input and output stream
  • FileReader, Filewriter etc...
  • readLine() reads ASCII/Unicode into a String
  • write () writes a String as ASCII/Unicode
  • newLine() writes an end of line marker in
    ASCII/Unicode

15
Reader Example
  • File fnew File("c/blob")
  • BufferedReader isnew BufferedReader(new
    FileReader(f))
  • String sis.readLine()
  • System.out.println(s)

16
Writer Example
  • File fnew File("c/blob")
  • BufferedWriter isnew BufferedWriter(new
    FileWriter(f))
  • is.write("hello world")
  • is.newLine()
  • is.flush()

17
reading and Writing Java types using Serialization
Input and Output Streams Section
18
Reading and Writing Your own Objects
  • We can read and write our own objects just as you
    and read and write a Java String
  • The class must implement Serializable
  • The class must have a constructor with no
    parameters
  • By default all attributes in the class are sent
    across the network this means any objects your
    object has references to are also sent
  • You can implement your own read and write methods
    or use transient to send only the attributes you
    need to

19
Serialisation Code Example
  • public class Hello implements Serializable
  • private String hello""
  • public Hello(String h)
  • helloh
  • public Hello()
  • hello""
  • public String message()
  • return hello

20
Selectively Serializing attributes
  • You can tell Java not to serialize an attribute
    by declaring it transient
  • private transient int temporary

21
Selectively Serializing attributes
  • You can write your own routines inside the class
    for serialising only what you need to
  • private void writeObject(java.io.ObjectOutputStrea
    m stream) throws IOException
  • stream.writeObject(hello)
  • private void readObject(java.io.ObjectInputStream
    stream) throws IOException, ClassNotFoundException
  • hello(String) stream.readObject()

22
Writing an Object
  • File fnew File("c/blob")
  • ObjectOutputStream isnew ObjectOutputStream(new
    FileOutputStream(f))
  • hello hnew hello("hello world")
  • is.writeObject(h)
  • is.flush()

23
How writing objects works
  • When you write an object one of several things
    can happen
  • If the object has no writeObject method then Java
    uses its own which writes all attributes which
    are not transient they must all be simple types
    or classes that also implement Serializable
  • If the object has its own writeObject method it
    is called and the default one is not used
  • In either case a version number based upon the
    objects code and attributes is also written to
    the stream

24
Reading an Object
  • File fnew File("c/blob")
  • ObjectInputStream isnew ObjectInputStream(new
    FileInputStream(f))
  • hello h(hello)is.readObject()
  • System.out.println(h.message())

25
How reading objects works
  • The object is constructed using the default
    constructor (no parameters)
  • The version of the object in the stream is
    checked against the version constructed. If the
    versions are not the same an Exception is
    generated
  • If the class has a readObject method it is called
  • If the class has no readObject method Java uses
    its own default method

26
Version Numbers
  • The version number is used to make sure that the
    version of the object in the stream is compatible
    with the compiled version
  • The problem is if you make any changes to the
    source code and recompile Java thinks the class
    is not compatible even if it is
  • You can bypass this behaviour by declaring the
    version of your object manually. however you have
    to check compatibility yourself
  • adding, removing or editing transient attributes
    does not cause a problem
  • adding, editing or removing methods does not
    cause a problem
  • adding, editing or removing new non transient
    attributes will cause a problem

27
Bypassing Version Checking
  • public class Hello implements Serializable
  • private static final long serialVersionUID1
  • private String hello""
  • public Hello(String h)
  • helloh
  • public Hello()
  • hello""
  • public String message()
  • return hello

28
More flexible Streams (Advanced)
Input and Output Streams Section
29
Random Access Streams
  • Until now all the streams we have looked at are
    sequential.
  • Writing starts at the beginning and bytes written
    can not be overwritten
  • Reading starts at the beginning and bytes read
    can not be re-read
  • If we want to be able to read or write any byte
    at any point in time we need a RandomAccess
    stream.
  • Some physical resources (such as keyboards) do
    not support randomAccess streams

30
Random Access File
  • The most common random access stream comes from a
    file.
  • This is because most modern operating systems
    support reading and writing any byte in any file
    at any time
  • Java uses the RandomAccessFile stream to read and
    write files. java can read and write
  • int, long, float, double

31
Random Access File
  • Although random access files implement dataInput
    and dataOutput to allow you to read and write
    standard Java data types the methods to move
    around the file move in units of bytes and NOT
    what ever you have written.
  • So if you write a file full of ints you need to
    move 4 bytes to access the next or previous int.
    This makes accurate positioning difficult

32
Moving Around
  • You can move to any byte inside the file using
    seek
  • You can find out the current byte using
    getFilePointer
  • You can move forward a certain number of bytes by
    using skip
  • The bytes are numbered sequentially the first
    being 0 and the last being length()-1

33
Writing a random access file
This is the access mode. r means read
only rw means read and write
  • RandomAccessFile fnew RandomAccessFile("c/blob",
    "rw")
  • f.writeChars("hello world")

writchars writes a string. But there is no
readchars you have to read then one at a time in
a loop!
34
Reading a random access file
  • RandomAccessFile fnew RandomAccessFile("c/blob"
    ,"r")
  • for (long if.length()-2igt0ii-2)
  • f.seek(i)
  • String s""f.readChar()
  • System.out.println(s)

each char is two bytes
start at the last char
finish at the first char (position 0)
Write a Comment
User Comments (0)
About PowerShow.com