Java IO Input and Output - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Java IO Input and Output

Description:

catch(IOException ei){ ei.printStackTrace(); try ... catch(IOException ei){ ei.printStackTrace(); Read / Write Example. Note use of try-catch ... – PowerPoint PPT presentation

Number of Views:346
Avg rating:3.0/5.0
Slides: 10
Provided by: ITS8213
Category:

less

Transcript and Presenter's Notes

Title: Java IO Input and Output


1
Lecture
  • Java I/O - Input and Output
  • java.io.

2
7. Java I/O
  • I/O in Java
  • Stream Classes
  • Streams vs Readers
  • Files
  • Read/Write Example
  • Serialization Examples

3
Java I/O Style
  • Java uses streams to handle byte and character
    I/O
  • A stream is an ordered source of or destination
    for bytes.
  • Java library includes pre-defined streams for
    dealing with common I/O operations
  • cin or stdin - System.in
  • cout or stdout - System.out
  • cerr or stderr - System.err
  • all streams information is contained in the
    java.io package
  • Streams can be connected to one another or to
    files
  • Input stream methods
  • int read()
  • int read(byte)
  • int read(byte, int, int)
  • close()
  • int available()
  • skip(long)
  • boolean markSupported()
  • mark(int)
  • reset()
  • Output stream methods
  • write(int)
  • write(byte)
  • write(byte, int, int)
  • close()
  • flush()

4
  • Stream Classes
  • BufferedInputStream and BufferedOutputStream
  • DataInputStream and DataOutputStream
  • PipedInputStream and PipedOutputStream
  • URL Input Streams
  • Streams vs. Readers
  • Streams are used for reading/writing ints/chars.
  • Readers/Writers are used for 16-bit Unicode
    characters.
  • Readers/Writers are in the java.io package.
  • Most important stream class InputStreamReader
    and OutputStreamWriter.
  • Local character encoding for English-speaking
    world is 8859_1
  • User can specify non-local character encodings
    when constructing the Reader/Writer

5
  • Java Files and I/O
  • Files and directories are represented in Java by
    the File class.
  • The File class contains information about the
  • file/directory
  • The File object can point to an absolute or
    relative directory.
  • The file separator is different depending on your
    platform (Unix, Microsoft Windows, etc.)
  • Java represents this by the static String
    separator in the java.io.File class
  • File methods
  • long lastModified()
  • long length()
  • boolean delete()
  • boolean exists()
  • boolean canRead()
  • boolean canWrite()
  • boolean isFile()
  • boolean isDirectory()
  • boolean isAbsolute()
  • String getName()
  • String getPath()
  • String getAbsolutePath()
  • String getParent()
  • boolean renameTo(File)

6
  • import java.util.
  • import java.io.
  • public class ReadWrite
  • public static void main( String args )
  • try
  • FileOutputStream f new FileOutputStream("m
    yfile.txt")
  • OutputStreamWriter osw new
    OutputStreamWriter(f)
  • BufferedWriter bw new BufferedWriter(osw)
  • bw.write("This is some text\n")
  • bw.write("second line\n")
  • bw.write("and third line\n")
  • bw.close()
  • osw.close()
  • f.close()
  • catch(FileNotFoundException ef)
  • System.out.println("Could not open file")
  • catch(IOException ei)
  • ei.printStackTrace()
  • Read / Write Example
  • Note use of try-catch
  • Example Output
  • gt java ReadWrite
  • Line 1This is some text
  • Line 2second line
  • Line 3and third line
  • gt

7
  • import java.util.
  • import java.io.
  • public class Serialize
  • public static void main( String args )
  • Wrapper w
  • try
  • FileInputStream f new FileInputStream("myf
    ile.ser")
  • ObjectInputStream s new
    ObjectInputStream(f)
  • w (Wrapper)s.readObject()
  • System.out.println("Read Wrapper stamped "
    w.timestamp )
  • s.close()
  • f.close()
  • catch(FileNotFoundException ef)
  • System.out.println("Could not open file")
  • catch(IOException ei)
  • ei.printStackTrace()
  • catch(ClassNotFoundException ec)
  • System.out.println("Could not extract
    Wrapper from myfile.ser")
  • Serialization Example
  • persistent objects of a sort
  • save to file or squirt down a network link
  • Example Output
  • gt rm myfile.ser
  • gt java Serialize
  • Could not open file
  • New Wrapper stamped Tue Jan 12 075447
    GMT1030 1999
  • gt java Serialize
  • Read Wrapper stamped Tue Jan 12 075447
    GMT1030 1999
  • New Wrapper stamped Tue Jan 12 075450
    GMT1030 1999
  • gt java Serialize
  • Read Wrapper stamped Tue Jan 12 075450
    GMT1030 1999
  • New Wrapper stamped Tue Jan 12 075453
    GMT1030 1999
  • gt

8
7. Exercises
  • Create a Java application called Viewer that can
    display the contents of any text file. The user
    should specify the file on the command line,
    using either a relative path or absolute path.
  • Create a Java application to copy a text file to
    a new file with a different name.
  • Modify the driver program you wrote in a previous
    section so that it has the ability to serialize
    and save the People array.
  • Add to your driver the ability to read in an
    existing database file and print out the
    contents.
  • Add a text-based menu system to your driver
    program giving the user options to load/save
    database files, and to add/search database
    entries.

9
Summary
  • Java I/O is done using Streams and with
    Readers and Writers
  • System.in/out etc
  • Serialization is built into the Java language
    see transient keyword
  • Streams are interoperable and can be stacked
    on top of one another to achieve desired user
    functionality
  • See java.io package
Write a Comment
User Comments (0)
About PowerShow.com