Java InputOutput - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Java InputOutput

Description:

The java.io package contains many classes that allow us to define various ... The FileReader class represents an input file containing character data ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 11
Provided by: jan8152
Category:

less

Transcript and Presenter's Notes

Title: Java InputOutput


1
Java Input/Output
  • Dave Elliman

2
I/O Streams
  • Java I/O is accomplished using objects that
    represent streams of data
  • A stream is a sequence of bytes that flow from a
    source to a destination. In Java a character is
    unicode or 2 bytes.
  • In a program, we read information from an input
    stream and write information to an output stream

3
I/O Streams
  • The java.io package contains many classes that
    allow us to define various streams with
    particular characteristics
  • Some classes assume that the data consists of
    characters
  • Others assume that the data consists of raw bytes
    of binary information

4
The IOException Class
  • Operations performed by the I/O classes may throw
    an IOException
  • Most likely because a file intended for reading
    or writing might not exist
  • You need a try/catch statement
  • try catch(Excepyion e)

5
Standard I/O Data Streams
  • There are three standard I/O streams
  • standard input defined by System.in
  • standard output defined by System.out
  • standard error defined by System.err
  • System.in typically represents keyboard input
  • System.out and System.err typically represent a
    particular window on the monitor screen
  • We use System.out when we execute println
    statements

6
Standard Input
  • InputStreamReader stdin new InputStreamReader(Sy
    stem.in)
  • BufferedReader in new BufferedReader(stdin)
  • String name in.readLine()
  • InputStreamReader converts the byte input stream
    to a character input stream
  • BufferedReader allows us to use the readLine
    method to get an entire line of input

7
Standard I/O example
  • import java.io.
  • public class TestIO
  • public static void main(String args)
  • double x
  • String y
  • BufferedReader in
  • new BufferedReader(new
    InputStreamReader(System.in))
  • try
  • System.out.print(Enter a floating
    point value )
  • x Double.parseDouble(in.readLine())
  • System.out.print(Enter some text
    )
  • y in.readLine()
  • System.out.println(x y)

8
Text Files
  • Information can be read from and written to text
    files by declaring and using the correct I/O
    streams
  • The FileReader class represents an input file
    containing character data
  • The FileWriter class represents a text output
    file, but with minimal support for manipulating
    data
  • Therefore use the PrintWriter class provides
    print and println methods

9
Text File I/O Streams
  • Input
  • BufferedReader in new BufferedReader(newFileRead
    er(in.txt))
  • In Java 1.5 the Scanner class makes life easier
    java.util.Scanner
  • Output
  • PrintWriter out new PrintWriter(new
    FileWriter(out.txt"))
  • streams should be closed explicitly
  • out.close()

10
That is all you need for PRG
  • But
  • You might like to learn about reading and writing
    binary files
  • Why not find out about reading and writing
    objects to file. This is known as serialization
Write a Comment
User Comments (0)
About PowerShow.com