SlideShare una empresa de Scribd logo
1 de 69
Descargar para leer sin conexión
Java Input/Output

11 April 2013	


OSU CSE	


1
Overview
•  The Java I/O (Input/Output) package
java.io contains a group of interfaces
and classes similar to the OSU CSE
components’ SimpleReader and
SimpleWriter component families
–  Except that java.io is far more general,
configurable, and powerful (and messy)
–  Hence, the names SimpleReader and
SimpleWriter

11 April 2013	


OSU CSE	


2
I/O Streams
•  An input/output stream is a (conceptually
not necessarily finite) series of data items
–  An input stream is a “flow” of data items from
a source to a program
•  The program reads from the source (or from the
stream)

–  An output stream is a “flow” of data items
from a program to a destination
•  The program writes to the destination (or to the
stream)
11 April 2013	


OSU CSE	


3
Input Streams
input stream program
source
single data item

11 April 2013	


OSU CSE	


4
Input Streams
input stream program
source
single data item

Source may be the keyboard, a file
on disk, a physical device, another
program, even an array or String
in the same program.
11 April 2013	


OSU CSE	


5
Output Streams
program output stream

destination

single data item

11 April 2013	


OSU CSE	


6
Output Streams
program output stream

destination

single data item

Destination may be the console
window, a file on disk, a physical
device, another program, even an array
or String in the same program.
11 April 2013	


OSU CSE	


7
Part I: Beginner’s Guide
•  This part is essentially a “how-to” guide for
using java.io that assumes knowledge
of the OSU CSE components’
SimpleReader and SimpleWriter
component families

11 April 2013	


OSU CSE	


8
Keyboard Input (SimpleReader)
•  Here’s some code in main to read input
from the keyboard, using SimpleReader:
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


9
Advice (except for the simplest
Keyboard guard against the user
Input (SimpleReader)
programs): to
entering “unexpected” input, read a line
• at a time into a String and then parse
Here’s some code in main to
it to see whether it looks like expected.

read input
from the keyboard, using SimpleReader:
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


10
Overview of java.io Input
Readable

Closeable

Reader

InputStreamReader

BufferedReader

There are more classes
and interfaces not
discussed here!

FileReader

11 April 2013	


OSU CSE	


11
Keyboard Input (java.io)
•  Here’s some code in main to read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


12
Keyboard Input (java.io)

• 

Some methods in java.io throw
exceptions (discussed later) under
certain circumstances, and you either
need some them in them
Here’s to catch codeor letmain to
propagate “up the call chain”, like this.

read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}

11 April 2013	


OSU CSE	


13
Keyboard Input (java.io)

The variable System.in is a Java
standard stream (discussed later), so
you may use it without declaring it; but it
•  is a byte stream (discussedmainso
Here’s some code in later), to
you want to wrap it ...

read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}

11 April 2013	


OSU CSE	


14
Keyboard Input (java.io)

... in a character stream (discussed
later) like this ...

•  Here’s some code in main to read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


15
Keyboard Input that
(java.io)
... and then you want to wrap
character stream in a buffered stream
(discussed later), like in main
•  Here’s some code this, so ... to

read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}

11 April 2013	


OSU CSE	


16
Keyboard Input (java.io)

... you can read one line at a time into a
String, like this.

•  Here’s some code in main to read input
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


17
Keyboard declared types of variables when
Input (java.io)
The
you use java.io are class types
rather main to read input
code in than interface types.

•  Here’s some
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


18
KeyboardThis technique of slightly extending
Input (java.io)to

• 

features or capabilities of an object by
wrapping it inside another object is a
Here’s somepopular object-orientedread input
code in main to design pattern
called the decorator pattern.
from the keyboard, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
...
input.close();
}

11 April 2013	


OSU CSE	


19
An Alternative (java.util)
•  An attractive alternative to using
java.io.BufferedReader is to use
java.util.Scanner
–  Important difference: no IOExceptions can
be raised, so you don’t need to worry about
catching or throwing them
–  Features for parsing input are powerful, e.g.,
the use of regular expressions to describe
delimiters between data items
11 April 2013	


OSU CSE	


20
An Alternative (java.util)
•  Here’s some code in main to read input
from the keyboard, using Scanner:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


21
Now, Alternative (java.util)
An main does not declare that it

throws IOException; in this sense it
is similar to using SimpleReader.

•  Here’s some code in main to read input
from the keyboard, using Scanner:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


22
An Alternative (java.util)

Notice that initialization does not involve
layers of wrapping of one object inside
another; Scanner also has different
• constructors so it can be used with files.
Here’s some code in main to

read input
from the keyboard, using Scanner:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


23
An Alternative (java.util)

The method for reading a line into a
String has a different name than for
BufferedReader: it is nextLine (like
•  Here’s some code in main to
SimpleReader).

read input
from the keyboard, using Scanner:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


24
End-of-Stream (SimpleReader)
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
while (!input.atEOS()) {
s = input.nextLine();
...
}
...
input.close();
}

11 April 2013	


OSU CSE	


25
End-of-Stream (SimpleReader)
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
while (!input.atEOS()) {
s = input.nextLine();
...
}
...
SimpleReader has a method to report “at
input.close();
end-of-stream”, and it can tell you this before
}

you read “past the end of the input stream”;
similarly, Scanner has method hasNext.

11 April 2013	


OSU CSE	


26
End-of-Stream (java.io)
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
while (s != null) {
...
s = input.readLine();
}
...
input.close();
}
11 April 2013	


OSU CSE	


27
End-of-Stream (java.io) to
BufferedReader has no method

detect when you cannot read any more
public static void main(String[] args)
input, so you can check it only after you
throws IOException {
try to read “past the end of the stream”.
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
while (s != null) {
...
s = input.readLine();
}
...
input.close();

}
11 April 2013	


OSU CSE	


28
End-of-Stream (java.io)
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new InputStreamReader(System.in));
String s = input.readLine();
while (s != null) {
Before checking again,
...
you must try to read
s = input.readLine();
another line.
}
...
input.close();
}
11 April 2013	


OSU CSE	


29
File Input (SimpleReader)
•  Here’s some code in main to read input
from a file, using SimpleReader:
public static void main(String[] args) {
SimpleReader input =
new SimpleReader1L("data/test.txt");
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


30
File Input (SimpleReader)
SimpleReader has a constructor from

a String, which is the name of the file
you want code in main to
•  Here’s some to read from.

read input
from a file, using SimpleReader:
public static void main(String[] args) {
SimpleReader input =
new SimpleReader1L("data/test.txt");
String s = input.nextLine();
...
input.close();
}

11 April 2013	


OSU CSE	


31
File Input (java.io)
•  Here’s some code in main to read input
from a file, using java.io:
public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new FileReader("data/test.txt"));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


32
File Input (java.io)

Now, you wrap a BufferedReader
around a FileReader (which has a
constructor from a String, which is the
•  name of the file you want to read from).
Here’s some code in main to

read input

from a file, using java.io:

public static void main(String[] args)
throws IOException {
BufferedReader input =
new BufferedReader(
new FileReader("data/test.txt"));
String s = input.readLine();
...
input.close();
}
11 April 2013	


OSU CSE	


33
Independence From Source
•  With SimpleReader, BufferedReader,
and Scanner used as shown, the source
is identified in a constructor call, and
subsequent code to read data is
independent of the source
–  This is a really handy feature in many
software maintenance situations
–  But notice: methods differ between
BufferedReader and the other two!
11 April 2013	


OSU CSE	


34
Console Output (SimpleWriter)
•  Here’s some code in main to write output
to the console, using SimpleWriter:
public static void main(String[] args) {
SimpleWriter output = new SimpleWriter1L();
output.print("foo");
output.println(" and bar");
...
output.close();
}

11 April 2013	


OSU CSE	


35
Both print
available,
Consoleand println are to output
Output (SimpleWriter)
and the latter should be used
the line separator string for the current
“platform” (i.e., code in main
•  Here’s somethe system the Javato
program is running on).

write output
to the console, using SimpleWriter:
public static void main(String[] args) {
SimpleWriter output = new SimpleWriter1L();
output.print("foo");
output.println(" and bar");
...
output.close();
}

11 April 2013	


OSU CSE	


36
Overview of java.io Output
Closeable

Flushable

Appendable

Writer

OutputStreamWriter

BufferedWriter

There are more classes
and interfaces not
discussed here!

FileWriter

11 April 2013	


PrintWriter

OSU CSE	


37
Console Output (java.io)
•  Here’s some code in main to write output
to the console, using java.io:
public static void main(String[] args) {
...
System.out.print("foo");
System.out.println(" and bar");
...
}

11 April 2013	


OSU CSE	


38
Console Output (java.io)

System.out is another Java standard
stream (discussed later), which you
neither declare nor close; both print
•  Here’s println are available. to
and some code in main

write output
to the console, using java.io:
public static void main(String[] args) {
...
System.out.print("foo");
System.out.println(" and bar");
...
}

11 April 2013	


OSU CSE	


39
Console Output (java.io)

It is fine to use System.out for console
output, but there are potential problems
using (unwrapped) System.in for
•  Here’s some code in main to write
keyboard input.

output

to the console, using java.io:

public static void main(String[] args) {
...
System.out.print("foo");
System.out.println(" and bar");
...
}

11 April 2013	


OSU CSE	


40
File Output (SimpleWriter)
•  Here’s some code in main to write output
to a file, using SimpleWriter:
public static void main(String[] args) {
SimpleWriter output =
new SimpleWriter1L("data/test.txt");
output.print("foo");
output.println(" and bar");
...
output.close();
}

11 April 2013	


OSU CSE	


41
File Outputa (SimpleWriter)
SimpleWriter has constructor from

a String, which is the name of the file
•  Here’s you wantcode in main to
some to write to.

write output
to a file, using SimpleWriter:
public static void main(String[] args) {
SimpleWriter output =
new SimpleWriter1L("data/test.txt");
output.print("foo");
output.println(" and bar");
...
output.close();
}

11 April 2013	


OSU CSE	


42
File Output (java.io)
•  Here’s some code in main to write output
to a file, using java.io:
public static void main(String[] args)
throws IOException {
PrintWriter output =
new PrintWriter(
new BufferedWriter (
new FileWriter("data/test.txt")));
output.print("foo");
output.println(" and bar");
...
output.close();
}
11 April 2013	


OSU CSE	


43
File is needed for (java.io)
Output convenient
PrintWriter

output, and is added as yet another
•  wrapper—to get print and println.
Here’s some code in main to

write output

to a file, using java.io:

public static void main(String[] args)
throws IOException {
PrintWriter output =
new PrintWriter(
new BufferedWriter (
new FileWriter("data/test.txt")));
output.print("foo");
output.println(" and bar");
...
output.close();
}
11 April 2013	


OSU CSE	


44
Independence From Destination
•  With SimpleWriter, System.out, and
PrintWriter used as shown, the
destination is identified in a constructor
call, and subsequent code to write data is
independent of the destination
–  This is a really handy feature in many
software maintenance situations
–  Unlike input, methods (print and println)
for all three have same names and behaviors
11 April 2013	


OSU CSE	


45
Part II: Some Details
•  There are way too many details about
java.io to cover here; see the Javadoc
and the Java Tutorials trail on “Basic I/O”
•  A few details previously promised ...

11 April 2013	


OSU CSE	


46
IOException
•  A general discussion of exceptions in
Java is to come later still, but for now…
•  A number of java.io constructors and
methods might throw (raise) an
IOException
–  Examples: files to be used as sources/
destinations may not exist, may not be
readable and/or writeable by the user of the
program, etc.
11 April 2013	


OSU CSE	


47
Try-Catch
•  As an alternative to letting an exception
propagate “up the call chain” to the client
(as in the earlier examples), you may deal
with an IOException by catching
(handling) it, e.g.:
–  Report that there has been an I/O error and
exit “gracefully”
–  Try to recover from it (which is usually much
harder)
11 April 2013	


OSU CSE	


48
Example
•  Here’s the overall structure of an example
main in a simple application that reads
input from a file, using java.io:
public static void main(String[] args) {
// Code to open file
// Code to read from file
// Code to close file
}

11 April 2013	


OSU CSE	


49
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
// Code to read from file
// Code to close file
}

11 April 2013	


OSU CSE	


50
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
Now, main does not declare that it
// Code to read from file
throws
// Code to close file IOException if it (always)
}
catches the exception.

11 April 2013	


OSU CSE	


51
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
This variable must be declared (but not
// Code to read from file
initialized) here, so it is in scope later
// Code to close file
where the code reads from the file—if the
}

file is opened successfully.

11 April 2013	


OSU CSE	


52
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
// Code to read from file
The try block contains the code that
// Code to close file
might throw an IOException ...
}

11 April 2013	


OSU CSE	


53
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
// Code to read from file
... which this constructor might throw,
// Code to close file
e.g., if the file does not exist.
}

11 April 2013	


OSU CSE	


54
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
The catch block states the exception it
// Code to read from file
handles, and is executed iff code in the
// Code to close file
try block throws that exception (which is
}

called e in the catch block).

11 April 2013	


OSU CSE	


55
Example: Opening a File
public static void main(String[] args) {
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
Here, the code prints an error message
// Code to read from file
to System.err, another Java standard
// Code to close file
stream (discussed later), and “aborts”
}

main by returning from it.

11 April 2013	


OSU CSE	


56
In eitherOpening the catch block,
(even after File
Example:didcasereturn asa does here),
if it
not
it
execution proceeds with {
public static void main(String[] args) the next
statement.
BufferedReader input;
try {
input = new BufferedReader(
new FileReader("data/test.txt"));
} catch (IOException e) {
System.err.println("Error opening file");
return;
}
// Code to read from file
// Code to close file
}

11 April 2013	


OSU CSE	


57
Example: Reading From a File
public static void main(String[] args) {
// Code to open file
try {
String s = input.readLine();
while (s != null) {
...
s = input.readLine();
}
} catch (IOException e) {
System.err.println("Error reading from file");
}
// Code to close file
}

11 April 2013	


OSU CSE	


58
We need this try-catch because
Example: Reading From throw the
a File
method readLine might
an
IOException.
public static void main(String[] args) {
// Code to open file
try {
String s = input.readLine();
while (s != null) {
...
s = input.readLine();
}
} catch (IOException e) {
System.err.println("Error reading from file");
}
// Code to close file
}
11 April 2013	


OSU CSE	


59
As before, even after the catch block
Example:(which in this caseFrom endFile
Reading does not a with a
return), execution args) {
public static void main(String[] proceeds with the
// Code to open file next statement.
try {
String s = input.readLine();
while (s != null) {
...
s = input.readLine();
}
} catch (IOException e) {
System.err.println("Error reading from file");
}
// Code to close file
}
11 April 2013	


OSU CSE	


60
Example: Closing a File
public static void main(String[] args) {
// Code to open file
// Code to read from file
try {
input.close();
} catch (IOException e) {
System.err.println("Error closing file");
}
}

11 April 2013	


OSU CSE	


61
We need this try-catch a File
Example: Closing because even the
method close might throw an
IOException.
public static void main(String[] args) {
// Code to open file
// Code to read from file
try {
input.close();
} catch (IOException e) {
System.err.println("Error closing file");
}
}

11 April 2013	


OSU CSE	


62
The Standard Streams
•  The utility class System in java.lang
declares three standard streams:
–  System.in
–  System.out
–  System.err

•  You do not declare, open, or close these
streams; but you can always use them
without worrying about exceptions
11 April 2013	


OSU CSE	


63
The Standard Streams
•  The utility class System in java.lang
declares three standard streams:
–  System.in
–  System.out
A utility class is a class that cannot be
–  System.errinstantiated, i.e., there is no public

constructor; it is or a type, these
•  You do not declare, open, not close so you
streams; but cannot create “an object of that type”.
you can always use them
without worrying about exceptions
11 April 2013	


OSU CSE	


64
The Standard Streams
System.err is intended for
•  The utility class System in java.lang
error messages; System.out is
declares three standard streams:
intended for normal output.

–  System.in
–  System.out
–  System.err

•  You do not declare, open, or close these
streams; but you can always use them
without worrying about exceptions
11 April 2013	


OSU CSE	


65
Byte and Character Streams
•  Java has two categories of streams:
–  Byte streams are streams of 8-bit bytes
•  This is a kind of low-level I/O that you rarely need

–  Character streams are streams of Unicode
characters
•  This is preferred for text I/O because it accounts
for the “local” character set and supports
internationalization with little additional effort

•  Best practice is to use character streams
with textual I/O
11 April 2013	


OSU CSE	


66
Buffered Streams
•  Buffered streams minimize disk access
for reading/writing files, and generally
have performance advantages over
unbuffered streams
•  Best practice is to “wrap” input and output
character streams to create buffered
versions
–  This is done with BufferedReader and
BufferedWriter, as seen earlier
11 April 2013	


OSU CSE	


67
Files and Paths
•  The File class (an original part of Java)
and the Path interface (new in Java 1.7)
allow you to manipulate directories and
files and the “paths” to them in the file
system, e.g.:
–  Check file existence and permissions, create
files and set permissions, delete files, etc.

•  See the Java Tutorials: Basic I/O and the
Java libraries’ Javadoc for details
11 April 2013	


OSU CSE	


68
Resources
•  The Java Tutorials: Basic I/O
–  http://docs.oracle.com/javase/tutorial/essential/io/index.html

11 April 2013	


OSU CSE	


69

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Files in java
Files in javaFiles in java
Files in java
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
Java IO
Java IOJava IO
Java IO
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java I/O
Java I/OJava I/O
Java I/O
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Java file
Java fileJava file
Java file
 
14 file handling
14 file handling14 file handling
14 file handling
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Io streams
Io streamsIo streams
Io streams
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 

Destacado

java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Introdução a Linguagem Java
Introdução a Linguagem JavaIntrodução a Linguagem Java
Introdução a Linguagem JavaUFPA
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operationsarchikabhatia
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingguesta40f80
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 

Destacado (20)

java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Introdução a Linguagem Java
Introdução a Linguagem JavaIntrodução a Linguagem Java
Introdução a Linguagem Java
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Managing console
Managing consoleManaging console
Managing console
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Slides chapters 6-7
Slides chapters 6-7Slides chapters 6-7
Slides chapters 6-7
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
file handling c++
file handling c++file handling c++
file handling c++
 

Similar a 32.java input-output

Similar a 32.java input-output (20)

sasasasasasas
sasasasasasassasasasasasas
sasasasasasas
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
Reading and writting
Reading and writtingReading and writting
Reading and writting
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
Streams&io
Streams&ioStreams&io
Streams&io
 
Files io
Files ioFiles io
Files io
 
Class IX Input in Java using Scanner Class (1).pptx
Class IX  Input in Java using Scanner Class (1).pptxClass IX  Input in Java using Scanner Class (1).pptx
Class IX Input in Java using Scanner Class (1).pptx
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Io stream
Io streamIo stream
Io stream
 

Más de santosh mishra (9)

My History
My HistoryMy History
My History
 
Biology
BiologyBiology
Biology
 
History
HistoryHistory
History
 
Biology
BiologyBiology
Biology
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
 

32.java input-output

  • 1. Java Input/Output 11 April 2013 OSU CSE 1
  • 2. Overview •  The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components’ SimpleReader and SimpleWriter component families –  Except that java.io is far more general, configurable, and powerful (and messy) –  Hence, the names SimpleReader and SimpleWriter 11 April 2013 OSU CSE 2
  • 3. I/O Streams •  An input/output stream is a (conceptually not necessarily finite) series of data items –  An input stream is a “flow” of data items from a source to a program •  The program reads from the source (or from the stream) –  An output stream is a “flow” of data items from a program to a destination •  The program writes to the destination (or to the stream) 11 April 2013 OSU CSE 3
  • 4. Input Streams input stream program source single data item 11 April 2013 OSU CSE 4
  • 5. Input Streams input stream program source single data item Source may be the keyboard, a file on disk, a physical device, another program, even an array or String in the same program. 11 April 2013 OSU CSE 5
  • 6. Output Streams program output stream destination single data item 11 April 2013 OSU CSE 6
  • 7. Output Streams program output stream destination single data item Destination may be the console window, a file on disk, a physical device, another program, even an array or String in the same program. 11 April 2013 OSU CSE 7
  • 8. Part I: Beginner’s Guide •  This part is essentially a “how-to” guide for using java.io that assumes knowledge of the OSU CSE components’ SimpleReader and SimpleWriter component families 11 April 2013 OSU CSE 8
  • 9. Keyboard Input (SimpleReader) •  Here’s some code in main to read input from the keyboard, using SimpleReader: public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 9
  • 10. Advice (except for the simplest Keyboard guard against the user Input (SimpleReader) programs): to entering “unexpected” input, read a line • at a time into a String and then parse Here’s some code in main to it to see whether it looks like expected. read input from the keyboard, using SimpleReader: public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 10
  • 11. Overview of java.io Input Readable Closeable Reader InputStreamReader BufferedReader There are more classes and interfaces not discussed here! FileReader 11 April 2013 OSU CSE 11
  • 12. Keyboard Input (java.io) •  Here’s some code in main to read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 12
  • 13. Keyboard Input (java.io) •  Some methods in java.io throw exceptions (discussed later) under certain circumstances, and you either need some them in them Here’s to catch codeor letmain to propagate “up the call chain”, like this. read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 13
  • 14. Keyboard Input (java.io) The variable System.in is a Java standard stream (discussed later), so you may use it without declaring it; but it •  is a byte stream (discussedmainso Here’s some code in later), to you want to wrap it ... read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 14
  • 15. Keyboard Input (java.io) ... in a character stream (discussed later) like this ... •  Here’s some code in main to read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 15
  • 16. Keyboard Input that (java.io) ... and then you want to wrap character stream in a buffered stream (discussed later), like in main •  Here’s some code this, so ... to read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 16
  • 17. Keyboard Input (java.io) ... you can read one line at a time into a String, like this. •  Here’s some code in main to read input from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 17
  • 18. Keyboard declared types of variables when Input (java.io) The you use java.io are class types rather main to read input code in than interface types. •  Here’s some from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 18
  • 19. KeyboardThis technique of slightly extending Input (java.io)to •  features or capabilities of an object by wrapping it inside another object is a Here’s somepopular object-orientedread input code in main to design pattern called the decorator pattern. from the keyboard, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 19
  • 20. An Alternative (java.util) •  An attractive alternative to using java.io.BufferedReader is to use java.util.Scanner –  Important difference: no IOExceptions can be raised, so you don’t need to worry about catching or throwing them –  Features for parsing input are powerful, e.g., the use of regular expressions to describe delimiters between data items 11 April 2013 OSU CSE 20
  • 21. An Alternative (java.util) •  Here’s some code in main to read input from the keyboard, using Scanner: public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 21
  • 22. Now, Alternative (java.util) An main does not declare that it throws IOException; in this sense it is similar to using SimpleReader. •  Here’s some code in main to read input from the keyboard, using Scanner: public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 22
  • 23. An Alternative (java.util) Notice that initialization does not involve layers of wrapping of one object inside another; Scanner also has different • constructors so it can be used with files. Here’s some code in main to read input from the keyboard, using Scanner: public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 23
  • 24. An Alternative (java.util) The method for reading a line into a String has a different name than for BufferedReader: it is nextLine (like •  Here’s some code in main to SimpleReader). read input from the keyboard, using Scanner: public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 24
  • 25. End-of-Stream (SimpleReader) public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); while (!input.atEOS()) { s = input.nextLine(); ... } ... input.close(); } 11 April 2013 OSU CSE 25
  • 26. End-of-Stream (SimpleReader) public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); while (!input.atEOS()) { s = input.nextLine(); ... } ... SimpleReader has a method to report “at input.close(); end-of-stream”, and it can tell you this before } you read “past the end of the input stream”; similarly, Scanner has method hasNext. 11 April 2013 OSU CSE 26
  • 27. End-of-Stream (java.io) public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); while (s != null) { ... s = input.readLine(); } ... input.close(); } 11 April 2013 OSU CSE 27
  • 28. End-of-Stream (java.io) to BufferedReader has no method detect when you cannot read any more public static void main(String[] args) input, so you can check it only after you throws IOException { try to read “past the end of the stream”. BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); while (s != null) { ... s = input.readLine(); } ... input.close(); } 11 April 2013 OSU CSE 28
  • 29. End-of-Stream (java.io) public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); while (s != null) { Before checking again, ... you must try to read s = input.readLine(); another line. } ... input.close(); } 11 April 2013 OSU CSE 29
  • 30. File Input (SimpleReader) •  Here’s some code in main to read input from a file, using SimpleReader: public static void main(String[] args) { SimpleReader input = new SimpleReader1L("data/test.txt"); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 30
  • 31. File Input (SimpleReader) SimpleReader has a constructor from a String, which is the name of the file you want code in main to •  Here’s some to read from. read input from a file, using SimpleReader: public static void main(String[] args) { SimpleReader input = new SimpleReader1L("data/test.txt"); String s = input.nextLine(); ... input.close(); } 11 April 2013 OSU CSE 31
  • 32. File Input (java.io) •  Here’s some code in main to read input from a file, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new FileReader("data/test.txt")); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 32
  • 33. File Input (java.io) Now, you wrap a BufferedReader around a FileReader (which has a constructor from a String, which is the •  name of the file you want to read from). Here’s some code in main to read input from a file, using java.io: public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new FileReader("data/test.txt")); String s = input.readLine(); ... input.close(); } 11 April 2013 OSU CSE 33
  • 34. Independence From Source •  With SimpleReader, BufferedReader, and Scanner used as shown, the source is identified in a constructor call, and subsequent code to read data is independent of the source –  This is a really handy feature in many software maintenance situations –  But notice: methods differ between BufferedReader and the other two! 11 April 2013 OSU CSE 34
  • 35. Console Output (SimpleWriter) •  Here’s some code in main to write output to the console, using SimpleWriter: public static void main(String[] args) { SimpleWriter output = new SimpleWriter1L(); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 35
  • 36. Both print available, Consoleand println are to output Output (SimpleWriter) and the latter should be used the line separator string for the current “platform” (i.e., code in main •  Here’s somethe system the Javato program is running on). write output to the console, using SimpleWriter: public static void main(String[] args) { SimpleWriter output = new SimpleWriter1L(); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 36
  • 37. Overview of java.io Output Closeable Flushable Appendable Writer OutputStreamWriter BufferedWriter There are more classes and interfaces not discussed here! FileWriter 11 April 2013 PrintWriter OSU CSE 37
  • 38. Console Output (java.io) •  Here’s some code in main to write output to the console, using java.io: public static void main(String[] args) { ... System.out.print("foo"); System.out.println(" and bar"); ... } 11 April 2013 OSU CSE 38
  • 39. Console Output (java.io) System.out is another Java standard stream (discussed later), which you neither declare nor close; both print •  Here’s println are available. to and some code in main write output to the console, using java.io: public static void main(String[] args) { ... System.out.print("foo"); System.out.println(" and bar"); ... } 11 April 2013 OSU CSE 39
  • 40. Console Output (java.io) It is fine to use System.out for console output, but there are potential problems using (unwrapped) System.in for •  Here’s some code in main to write keyboard input. output to the console, using java.io: public static void main(String[] args) { ... System.out.print("foo"); System.out.println(" and bar"); ... } 11 April 2013 OSU CSE 40
  • 41. File Output (SimpleWriter) •  Here’s some code in main to write output to a file, using SimpleWriter: public static void main(String[] args) { SimpleWriter output = new SimpleWriter1L("data/test.txt"); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 41
  • 42. File Outputa (SimpleWriter) SimpleWriter has constructor from a String, which is the name of the file •  Here’s you wantcode in main to some to write to. write output to a file, using SimpleWriter: public static void main(String[] args) { SimpleWriter output = new SimpleWriter1L("data/test.txt"); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 42
  • 43. File Output (java.io) •  Here’s some code in main to write output to a file, using java.io: public static void main(String[] args) throws IOException { PrintWriter output = new PrintWriter( new BufferedWriter ( new FileWriter("data/test.txt"))); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 43
  • 44. File is needed for (java.io) Output convenient PrintWriter output, and is added as yet another •  wrapper—to get print and println. Here’s some code in main to write output to a file, using java.io: public static void main(String[] args) throws IOException { PrintWriter output = new PrintWriter( new BufferedWriter ( new FileWriter("data/test.txt"))); output.print("foo"); output.println(" and bar"); ... output.close(); } 11 April 2013 OSU CSE 44
  • 45. Independence From Destination •  With SimpleWriter, System.out, and PrintWriter used as shown, the destination is identified in a constructor call, and subsequent code to write data is independent of the destination –  This is a really handy feature in many software maintenance situations –  Unlike input, methods (print and println) for all three have same names and behaviors 11 April 2013 OSU CSE 45
  • 46. Part II: Some Details •  There are way too many details about java.io to cover here; see the Javadoc and the Java Tutorials trail on “Basic I/O” •  A few details previously promised ... 11 April 2013 OSU CSE 46
  • 47. IOException •  A general discussion of exceptions in Java is to come later still, but for now… •  A number of java.io constructors and methods might throw (raise) an IOException –  Examples: files to be used as sources/ destinations may not exist, may not be readable and/or writeable by the user of the program, etc. 11 April 2013 OSU CSE 47
  • 48. Try-Catch •  As an alternative to letting an exception propagate “up the call chain” to the client (as in the earlier examples), you may deal with an IOException by catching (handling) it, e.g.: –  Report that there has been an I/O error and exit “gracefully” –  Try to recover from it (which is usually much harder) 11 April 2013 OSU CSE 48
  • 49. Example •  Here’s the overall structure of an example main in a simple application that reads input from a file, using java.io: public static void main(String[] args) { // Code to open file // Code to read from file // Code to close file } 11 April 2013 OSU CSE 49
  • 50. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } // Code to read from file // Code to close file } 11 April 2013 OSU CSE 50
  • 51. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } Now, main does not declare that it // Code to read from file throws // Code to close file IOException if it (always) } catches the exception. 11 April 2013 OSU CSE 51
  • 52. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } This variable must be declared (but not // Code to read from file initialized) here, so it is in scope later // Code to close file where the code reads from the file—if the } file is opened successfully. 11 April 2013 OSU CSE 52
  • 53. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } // Code to read from file The try block contains the code that // Code to close file might throw an IOException ... } 11 April 2013 OSU CSE 53
  • 54. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } // Code to read from file ... which this constructor might throw, // Code to close file e.g., if the file does not exist. } 11 April 2013 OSU CSE 54
  • 55. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } The catch block states the exception it // Code to read from file handles, and is executed iff code in the // Code to close file try block throws that exception (which is } called e in the catch block). 11 April 2013 OSU CSE 55
  • 56. Example: Opening a File public static void main(String[] args) { BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } Here, the code prints an error message // Code to read from file to System.err, another Java standard // Code to close file stream (discussed later), and “aborts” } main by returning from it. 11 April 2013 OSU CSE 56
  • 57. In eitherOpening the catch block, (even after File Example:didcasereturn asa does here), if it not it execution proceeds with { public static void main(String[] args) the next statement. BufferedReader input; try { input = new BufferedReader( new FileReader("data/test.txt")); } catch (IOException e) { System.err.println("Error opening file"); return; } // Code to read from file // Code to close file } 11 April 2013 OSU CSE 57
  • 58. Example: Reading From a File public static void main(String[] args) { // Code to open file try { String s = input.readLine(); while (s != null) { ... s = input.readLine(); } } catch (IOException e) { System.err.println("Error reading from file"); } // Code to close file } 11 April 2013 OSU CSE 58
  • 59. We need this try-catch because Example: Reading From throw the a File method readLine might an IOException. public static void main(String[] args) { // Code to open file try { String s = input.readLine(); while (s != null) { ... s = input.readLine(); } } catch (IOException e) { System.err.println("Error reading from file"); } // Code to close file } 11 April 2013 OSU CSE 59
  • 60. As before, even after the catch block Example:(which in this caseFrom endFile Reading does not a with a return), execution args) { public static void main(String[] proceeds with the // Code to open file next statement. try { String s = input.readLine(); while (s != null) { ... s = input.readLine(); } } catch (IOException e) { System.err.println("Error reading from file"); } // Code to close file } 11 April 2013 OSU CSE 60
  • 61. Example: Closing a File public static void main(String[] args) { // Code to open file // Code to read from file try { input.close(); } catch (IOException e) { System.err.println("Error closing file"); } } 11 April 2013 OSU CSE 61
  • 62. We need this try-catch a File Example: Closing because even the method close might throw an IOException. public static void main(String[] args) { // Code to open file // Code to read from file try { input.close(); } catch (IOException e) { System.err.println("Error closing file"); } } 11 April 2013 OSU CSE 62
  • 63. The Standard Streams •  The utility class System in java.lang declares three standard streams: –  System.in –  System.out –  System.err •  You do not declare, open, or close these streams; but you can always use them without worrying about exceptions 11 April 2013 OSU CSE 63
  • 64. The Standard Streams •  The utility class System in java.lang declares three standard streams: –  System.in –  System.out A utility class is a class that cannot be –  System.errinstantiated, i.e., there is no public constructor; it is or a type, these •  You do not declare, open, not close so you streams; but cannot create “an object of that type”. you can always use them without worrying about exceptions 11 April 2013 OSU CSE 64
  • 65. The Standard Streams System.err is intended for •  The utility class System in java.lang error messages; System.out is declares three standard streams: intended for normal output. –  System.in –  System.out –  System.err •  You do not declare, open, or close these streams; but you can always use them without worrying about exceptions 11 April 2013 OSU CSE 65
  • 66. Byte and Character Streams •  Java has two categories of streams: –  Byte streams are streams of 8-bit bytes •  This is a kind of low-level I/O that you rarely need –  Character streams are streams of Unicode characters •  This is preferred for text I/O because it accounts for the “local” character set and supports internationalization with little additional effort •  Best practice is to use character streams with textual I/O 11 April 2013 OSU CSE 66
  • 67. Buffered Streams •  Buffered streams minimize disk access for reading/writing files, and generally have performance advantages over unbuffered streams •  Best practice is to “wrap” input and output character streams to create buffered versions –  This is done with BufferedReader and BufferedWriter, as seen earlier 11 April 2013 OSU CSE 67
  • 68. Files and Paths •  The File class (an original part of Java) and the Path interface (new in Java 1.7) allow you to manipulate directories and files and the “paths” to them in the file system, e.g.: –  Check file existence and permissions, create files and set permissions, delete files, etc. •  See the Java Tutorials: Basic I/O and the Java libraries’ Javadoc for details 11 April 2013 OSU CSE 68
  • 69. Resources •  The Java Tutorials: Basic I/O –  http://docs.oracle.com/javase/tutorial/essential/io/index.html 11 April 2013 OSU CSE 69