Download ppt

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Air Force Institute of Technology
Electrical and Computer Engineering
Object-Oriented Programming Design
Topic :
Streams and Files
Maj Joel Young
Joel [email protected].
29-Apr-17
Maj Joel Young
Object-Oriented
Programming
Design
Java Input/Output
• Java implements input/output in terms of streams of
characters
– Streams have a producer, or source
– Streams have a consumer, or sink
– Sometimes sources are sinks depending on use
(a file can be both a source and a sink)
Source
programs, user
input, etc.
29-Apr-17
Sink
files, console,
sockets, etc.
Air Force Institute of Technology
Electrical and Computer Engineering
2
Object-Oriented
Programming
Design
I/O Processors
• Control over the stream is improved by adding processors
– Convert char streams to integers, doubles, strings, etc.
– Perform filtering
– Buffer characters to improve read performance
Source
29-Apr-17
Processor
Air Force Institute of Technology
Electrical and Computer Engineering
Sink
3
Object-Oriented
Programming
Design
Data Sinks/Sources
• Several kinds of data streams in Java
File
Reader
File
Writer
Pipe
Reader
Processor(s)
String
Reader
Processor(s)
String
Writer
Char
Array
Reader
Char
Array
Writer
Input Streams
(Application is Sink)
29-Apr-17
Pipe
Writer
Output Streams
(Application is Source)
Air Force Institute of Technology
Electrical and Computer Engineering
4
Object-Oriented
Programming
Design
The “Pipeline” Concept
• Writing Data: Start with a sink, such as a FileOutputStream
• Writes only one byte at a time (or an array of bytes), not
very efficient – so we add a buffer manager
• Can still only write a byte (or array of bytes), so we add a
DataOutputStream to allow more complex types
double,
int, char,
String, etc.
29-Apr-17
bytes
Data
OutputStream
bytes
Buffered
OutputStream
Air Force Institute of Technology
Electrical and Computer Engineering
bytes
File
OutputStream
Hard
Disk
5
Object-Oriented
Programming
Design
The “Pipeline” Concept
• Reading Data: Start with a source, such as a
FileInputStream
• Reads only one byte at a time (or an array of bytes), not very
efficient – so we add a buffer manager
• Can still only read a byte (or array of bytes), so we add a
DataInputStream to allow more complex types
double,
int, char,
String, etc.
29-Apr-17
bytes
Data
InputStream
bytes
Buffered
InputStream
Air Force Institute of Technology
Electrical and Computer Engineering
bytes
File
InputStream
Hard
Disk
6
Object-Oriented
Programming
Design
File Streams
• File Streams (Byte Stream Classes)
– FileInputStream: read bytes/arrays of bytes
– FileOutputStream: write bytes/arrays of bytes
import java.io.*;
public class Copy
{
public static void main(String[] args) throws IOException
{
FileInputStream in = new FileInputStream(“source.dat”);
FileOutputStream out = new FileOutputStream(“dest.dat”);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
7
Object-Oriented
Programming
Design
File Streams
• Add DataInputStream/DataOutputStream
– Provides read/write methods for primitives
– Provides read/write methods for unicode strings
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream(“output.dat”);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeDouble(64.356);
dos.close();
FileInputStream fis = new FileInputStream(“output.dat”);
DataInputStream dis = new DataInputStream(fis);
double test = dis.readDouble();
System.out.println( test );
dis.close();
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
8
Object-Oriented
Programming
Design
File Streams
• Can keep adding processors ...
• Add buffered input/output
import java.io.*;
public class Test2
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos = new FileOutputStream(“output.dat”);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeDouble(64.356);
dos.close();
FileInputStream fis = new FileInputStream(“output.dat”);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
double test = dis.readDouble();
System.out.println( test );
dis.close();
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
9
Object-Oriented
Programming
Design
File Streams
• File Streams (Character Stream Classes)
– FileReader: read chars/arrays of chars
– FileWriter: write chars/arrays of chars
import java.io.*;
public class Copy2
{
public static void main(String[] args) throws IOException
{
FileReader in = new FileReader(“source.dat”);
FileWriter out = new FileWriter(“dest.dat”);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
10
Object-Oriented
Programming
Design
ASCII Files
• Problem: ASCII uses 8-bit chars, but Java uses 16-bit
Unicode chars – how do we read plain-text files?
– InputStreamReader: Translates input bytes to Unicode
public class Copy
{
public static void main(String[] args) throws IOException
{
FileInputStream in = new FileInputStream(“source.dat”);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
// Read a line of text
while (line != null)
{
System.out.println(line);
line = br.readLine();
}
br.close();
// Any more text to read?
// Print the text to the console
// Read more text
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
11
Object-Oriented
Programming
Design
ASCII Formatted Numbers &
Variable Length Strings
class Test
{
static public void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(“test.dat”)));
String line = br.readLine(); // Get first line of text
StringTokenizer st;
int studentNum;
1 4.0 Smith, Joe
double gradeAvg;
2 2.8 Jones, Jim Bob
String name;
3 3.9 Doe, Tricia
while (line != null)
. . .
{
st = new StringTokenizer(line);
studentNum = Integer.parseInt(st.nextToken());
gradeAvg = Double.parseDouble(st.nextToken());
name = st.nextToken();
while (st.hasMoreTokens())
{
name = name + “ “ + st.nextToken();
}
System.out.println(studentNum+”,”+gradeAvg+”,”+name);
line = br.readLine();
}
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
12
Object-Oriented
Programming
Design
Console I/O
• PrintStream
– Heavily overloaded versions of print() for:
– print(int n)
– print(double d)
– print(float f)
– print(String s)
– …
– Version called println() puts a newline character after the formatted text
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
13
Object-Oriented
Programming
Design
System.in & System.out
• Like C++ keeps “stdin” and “stdout” streams open at all
times
– System.in is a class attribute of type InputStream
– System.out is a class attribute of type PrintStream
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
System.out.print(“Here’s a string: ”);
System.out.println(10); // Integer 10 w/new line break
}
}
29-Apr-17
Air Force Institute of Technology
Electrical and Computer Engineering
14
Related documents