IO Basics - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

IO Basics

Description:

To create, need to pass it who it will print to: PrintWriter pw; pw = new PrintWriter (fos) ... We create a Socket, which is a connection between our machine ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: JeffCh2
Category:

less

Transcript and Presenter's Notes

Title: IO Basics


1
I/O Basics
2
I/O
  • There are several places to read information
  • Hard Drive
  • Network
  • Keyboard
  • There are several places to write information
  • Hard Drive
  • Network
  • Printer or Screen
  • When working with I/O several things throw
    exceptions!

3
The General Modelfor Input
KeyBoard
Files
The Net
The Keyboard, Files and Network work at the
byte-level!
Convert toCharacters
Convert toStrings
We want to work with Strings, not bytes!
4
InputStreams
KeyBoard
Files
The Net
  • To read from the Keyboard, start with System.in
  • To read from files, start with a FileInputStream
  • To read from the network, create a Socket then
    get its InputStream!

System.in
Sockets
FileInputStream
5
InputStreams
  • All InputStreams have the ability to read
  • int read ( )
  • Reads the next byte of data from the input
    stream
  • int read (byte b)
  • Reads some number of bytes from the input
    stream into an array of bytes
  • Several different read methods

6
Bad ExampleWorking with the Keyboard
  • import java.io.
  • public class BadExample
  • public static void main (String args )
  • int userVal 0
  • System.out.println (Enter your name)
  • // Try reading their name Jeff
  • try
  • userVal System.in.read( )
  • catch (IOException e)
  • // Prints out only 74 the ASCII value of
    J
  • System.out.println (userVal)

7
Bad Example 2Better, but still bad!
  • import java.io.
  • public class BadExample2
  • public static void main (String args )
  • int userVal 0
  • System.out.println (Enter your name)
  • while (userVal ! -1)
  • // Try reading their name Jeff
  • try
  • userVal System.in.read( )
  • catch (IOException e)
  • // Prints out only 74, 101, 102, 102, 10
  • System.out.println (userVal)
  • // Note the 10 is from when they hit Enter
    or Return

8
The Problem
  • We have the ability to read in numbers from the
    user, but want to work with Strings!
  • We need to convert the numbers to characters
    (using an InputStreamReader)
  • Then, we need to convert the characters into
    Strings (using a BufferedReader)
  • This way, we only have to work with the
    BufferedReader, and not System.in or the
    InputStreamReader!

9
InputStreamReaders
  • To instantiate an InputStreamReader, you need to
    tell it where it is getting its data from!
  • In this case, the ISR is getting data from the
    Keyboard (System.in), SO
  • InputStreamReader isr
  • isr new InputStreamReader (System.in)

10
BufferedReaders
  • BufferedReaders read a series of characters and
    hold on (buffer) them.
  • BufferedReaders have a readLine( ) method! (no
    more working with bytes)
  • To instantiate a BufferedReader, you need to tell
    it where it is reading data from
  • BufferedReader br
  • // Tell br to read from our InputStreamReader
  • br new BufferedReader (isr)

11
Better Example
  • import java.io.
  • public class Echo
  • public static void main (String args )
  • InputStreamReader isr
  • BufferedReader br
  • String userVal
  • System.out.println (Enter your name)
  • try
  • // Tell isr to read from the Keyboard
  • isr new InputStreamReader (System.in)
  • // Tell br to read from isr
  • br new BufferedReader (isr)
  • userVal br.readLine( )
  • catch (IOException e)
  • // Prints out the users name
  • System.out.println (userVal)

12
SO
  • When we read from br, it reads from isr
  • When br reads from isr, isr reads from System.in
    (the Keyboard)
  • Basically, a lot of translation is going on!

13
Reading from Files
  • Work with the FileInputStream class
  • Remember, FileInputStream alone works with bytes!
  • Uses the exact same model as working with
    Keyboard (still need ISR and BR)
  • Create a FileInputStream by telling it the name
    of the file
  • FileInputStream fis
  • fis new FileInputStream (bob.txt)

14
Reading from Files
  • import java.io.
  • public class More
  • public static void main (String args )
  • FileInputStream fis
  • InputStreamReader isr
  • BufferedReader br
  • String userVal
  • try
  • fis new FileInputStream (bob.txt)
  • // Tell isr to read from the fis
  • isr new InputStreamReader (fis)
  • br new BufferedReader (isr)
  • userVal br.readLine( )
  • while (userVal ! null)
  • System.out.println (userVal)
  • userVal br.readLine( )
  • catch (IOException e)

15
The ModelOutput
  • Note System.out is a PrintWriter, and is already
    set up at runtime

System.out
FileOutputStream
Socket (network)
PrintWriter
16
OutputStreams
  • All OutputStreams have the ability to write
  • void write (int b)
  • Writes the specified byte to this output stream
  • void write (byte b)
  • Writes b.length bytes from the specified byte
    array to this output stream

17
FileOutputStreams
  • Opposite of FileInputStream, FOSs write to files!
  • To create, pass the name of the file, and whether
    or not to append to the end of the file
  • Same problem as System.in and FileInputStream
    works with bytes only!
  • FileOutputStream fos
  • // Connect to bob.txt and destroy what was there
  • fos new FileOutputStream (bob.txt, false)

18
The PrintWriter
  • PrintWriters have the ability to print Strings
    directly to lower-level classes
  • System.out is a PrintWriter that prints to the
    screen!
  • Supports both print( ) and println ( ) methods
    (plus several others)
  • To create, need to pass it who it will print to
  • PrintWriter pw
  • pw new PrintWriter (fos)

19
Flushing and Closing
  • Because it is more efficient to send big block of
    text than individual characters, there can be
    problems.
  • Most of the time, you need to tell the
    PrintWriter to flush, meaning send what you
    have
  • Quote Dont forget to flush!
  • Also, you should close your streams when you are
    finished with them

20
Writing to a File
  • import java.io.
  • public class FileExample
  • public static void main (String args )
  • FileOutputStream fos
  • PrintWriter pw
  • try
  • // Hook fos up to the file bob.txt
  • fos new FileOutputStream (bob.txt)
  • // Tell pw to write to fos, which writes to
    bob.txt
  • pw new PrintWriter (fos)
  • // Write data
  • pw.println (Bob was here)
  • pw.println (and hes mad)
  • // Thanks mom!
  • pw.flush( )
  • pw.close( )
  • catch (IOException e)

21
Extra Credit The Poor Mans Web Browser
  • Reading a web page from the internet is almost
    the same as reading from a file.
  • We create a Socket, which is a connection between
    our machine and another
  • Then, get the InputStream (for reading a web
    page) and OutputStream (to send the request for a
    page)
  • Need to import java.net.
  • To create a Socket, you need to specify a machine
    to hook up to, and a port
  • Socket s
  • s new Socket (kahuna.clayton.edu, 80)

22
  • import java.net.
  • import java.io.
  • public class PoorMansWebBrowser
  • public static void main (String args )
  • Socket s
  • OutputStream os
  • InputStream is
  • InputStreamReader isr
  • BufferedReader br
  • PrintWriter pw
  • String htmlLine
  • try
  • s new Socket ("kahuna.clayton.edu",
    80) // Create the socket
  • os s.getOutputStream()
    // Get the OutputStream
  • is s.getInputStream()
    // Get the InputStream
  • isr new
    InputStreamReader (is) // isr writes to the
    net!
  • br new BufferedReader
    (isr) // br writes to isr
  • pw new PrintWriter
    (os) // pw writes to the net!
  • pw.println ("GET
    /index.html HTTP/1.0 \n\n") // send request for
    page

23
Summary
  • Input always works the same way
  • Start out with byte stream (keyboard, files, net)
  • Convert bytes into chars (InputStreamReader)
  • Convert chars into Strings (BufferedReader)
  • Output always works the same way
  • Start with byte stream (files or net)
  • Print Strings to that stream (PrintWriter)

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com