Title: I/O in Java
1I/O in Java
- Dennis Burford
- dburford_at_cs.uct.ac.za
2Input and Output
- Obtaining input
- Using BasicIo methods.
- Through a GUI
- Producing output
- Using System.out methods (System.out.println())
- Through a GUI
- Other input/output files, sockets etc.
3Streams
- Java uses the concept of Streamsfor I/O
- Data flows from a Writer to a Reader
Stream
Reader
Writer
4Why Streams?
- Stream generalizes input output
- Keyboard electronics different from disk
- Input stream makes keyboard and files seem the
same to a Reader
Input Stream
Reader
5Simplified Keyboard Input
- Java First Contact uses BasicIo class
- String name BasicIo.readString()
- int age BasicIo.readInteger()
- Slack textbook uses Keyboard class
- String name Keyboard.readString()
- int age Keyboard.readInt()
6Simplified Keyboard Input
- Both BasicIo and Keyboard are wrapper classes
used to hide detail. - In reality, both classes use System.in and other
low-level Java classes to operate.
7System class
- System class is inside java.lang package
- Contains 2 variables
- public static final InputStream in
- public static final PrintStream out
- System.in is usually connected to the users
keyboard. - System.out is usually connected to the users
screen.
8Using System.in
- System.in is an InputStream
- Can use System.in many ways
- Directly (low-level access)
- Through layers of abstraction (high-level access)
9Using System.in
- read() method reads one character at a time.
- Returns an int
- Returns 1 for EOF (End of File Ctrl-Z)
10Using System.in
- public static void main(String args)
- throws java.io.IOException
-
- char character
- // Prompt for a character and read it
- System.out.print("Enter a character ")
- System.out.flush()
- character (char) System.in.read()
-
- // Display the character typed
- System.out.println()
- System.out.println("You typed " character)
-
11Using System.in
- int intChar System.in.read()
- while (intChar ! -1)
-
- // Convert to character
- char character (char) intChar
- System.out.println("Next character is "
character) - // Get next one
- intChar System.in.read()
12Reading Strings
- No String-reading methods in System.in
- To read strings from keyboard, first wrap
System.in inside InputStreamReader object - InputStreamReader ISReader
- new InputStreamReader(System.in)
13Reading Strings
14Reading Strings
- Next, wrap InputStreamReader object in
BufferedReader object - InputStreamReader ISReader
- new InputStreamReader(System.in)
- BufferedReader BufReader
- new BufferedReader(ISReader)
15Reading Strings
- Can combine these into a single statement
BufferedReader BufReader new
BufferedReader(new InputStreamReader(System.in))
16Reading Strings
- Can combine these into a single statement
BufferedReader BufReader new
BufferedReader(new InputStreamReader(System.in))
InputStream
InputStreamReader
BufferedReader
17Reading Strings
- Methods in BufferedReader
- read()
- readLine()
- Read a string by using readLine() method
- String str BufReader.readLine()
18Reading Strings
19Reading Strings
- InputStreamReader, BufferedReader in java.io.
- Skeleton for reading
- import java.io.
- class ClassName
-
- public static void main(String args)
- throws java.io.IOException
-
- // Create a buffered input stream and attach
it to standard - // input
- BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in)) - ...
-
20Example Reading Users Name
- // Reads a user's first name and last name.
- // Demonstrates use of InputStreamReader,
- // BufferedReader and the readLine() method.
- import java.io.
- class ReadInputAsString
-
- public static void main(String args)
- throws java.io.IOException
-
- String firstName, lastName
-
- // Create an input stream and attach it to
the standard - // input stream
- BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in))
21Example Reading Users Name
- // Read a line from the user as a String
- System.out.print("Enter your first name ")
- System.out.flush()
- firstName BufReader.readLine()
-
- // Read a line from the user as a String
- System.out.print("Enter your last name ")
- System.out.flush()
- lastName BufReader.readLine()
- // Display the strings
- System.out.println()
- System.out.println("Your name is" firstName
lastName) -
-
22Example Reading Users Name
- // Read a line from the user as a String
- System.out.print("Enter your first name ")
- System.out.flush()
- firstName BufReader.readLine()
-
- // Read a line from the user as a String
- System.out.print("Enter your last name ")
- System.out.flush()
- lastName BufReader.readLine()
- // Display the strings
- System.out.println()
- System.out.println("Your name is" firstName
lastName) -
-
Enter your first name Linda Enter your last
name Jones Your name is Linda Jones
23Formatting Numbers
- Alternative to Integer.parseInt() etc.
- NumberFormat
- Object factory use getInstance()
- parse() takes a string and returns a Number
- parse() throws java.text.ParseException
- General Number class
- Return value using intValue(), doubleValue() etc.
24(No Transcript)
25Formatting Numbers from Kbd
- NumberFormat formatter NumberFormat.getInstance(
) - BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in)) - System.out.print("Enter an integer ")
- System.out.flush()
- String response BufReader.readLine()
- Number numberObj formatter.parse(response)
- int numberInt numberObj.intValue()
26Formatting Numbers from Kbd
- NumberFormat formatter NumberFormat.getInstance(
) - BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in)) - System.out.print("Enter an integer ")
- System.out.flush()
- String response BufReader.readLine()
- Number numberObj formatter.parse(response)
- int numberInt numberObj.intValue()
Combine
27Formatting Numbers from Kbd
- NumberFormat formatter NumberFormat.getInstance(
) - BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in)) - System.out.print("Enter an integer ")
- System.out.flush()
- int numberInt
- formatter.parse( BufReader.readLine()
).intValue()
28Formatting Numbers from Kbd
- NumberFormat formatter NumberFormat.getInstance(
) - BufferedReader BufReader
- new BufferedReader(new InputStreamReader(S
ystem.in)) - System.out.print("Enter an integer ")
- System.out.flush()
- int numberInt
- formatter.parse( BufReader.readLine()
).intValue()
Dont forget to throw or catch java.text.ParseExce
ption
29Input Streams Multiple Values
- To read several values on one line
- Use StringTokenizer object
- Breaks one string into component parts
- Must still convert numbers (if necessary)
- StringTokenizer
- Constructor takes string
- nextToken() method
- hasMoreTokens() method
30(No Transcript)
31Input Streams Multiple Values
- StringTokenizer tokenizer new
StringTokenizer("42 58") - String token
- token tokenizer.nextToken()
- System.out.println(token) // Displays 42
- token tokenizer.nextToken()
- System.out.println(token) // Displays 58
- BETTER
- StringTokenizer tokenizer new
StringTokenizer("42 58") - while (tokenizer.hasMoreTokens())
-
- System.out.println(tokenizer.nextToken())
32FILES
33Overview of Files
- How they are accessed
- sequential data items must be accessed in the
order in which they are stored (ie. start at the
beginning and pass through all the items) - direct (or random) items are accessed by
specifying their location - How information is represented
- text files data is stored in character form.
- binary files data is stored in internal binary
form (faster and more compact than text files).
34Types of Files
Summer.txt
Rough winds do shake the darling buds of
May\n And Summers lease hath all too short a
date\n Sometime too hot the eye of heaven
shines,\n And oft is his gold complexion
dimmed,\n And every fair from fair sometime
declines...
Numbers.dat
Œpôw10Žélú9câÜ(3xLenfˆx¹ª(Ͻ¼øßµ