Introduction to Programming Gordon College Streams

Preview:

Citation preview

Introduction to Programming

Gordon College

Streams

Psal 1:1 Blessed is the man who does not walk in the counsel of the wicked or stand in the way of sinners or sit in the seat of mockers. But his delight is in the law of the LORD, and on his law he meditates day and night. He is like a tree planted by streams of water, which yields its fruit in season and whose leaf does not wither. Whatever he does prospers.

ffd8ffe000104a464946000101000100010000ffdb00430006

Stream.jpg

Camera to file

ffd8ffe000104a464946000101000100010000ffdb00430006

Stream.jpg

File to Laptop

ffd8ffe000104a464946000101000100010000ffdb00430006

Laptop to Laptop

Outline

Data Hierarchy What’s a Stream Different Types of Streams Text Streams Meta-File Information Binary Files and Random Files Accessing Pages Through a Network

Data Hierarchy

What’s a Stream

Managed flow of data between programs & any external entity that can provide or receive data.

Java stream-processing capabilities:

1. read & write data in files

2. read & write data in memory

3. read & write data over network connections

Different Types of Streams

1. Direction of streamWhich way does the stream flow? input stream - from file to process output stream - from process to file

`

Different Types of Streams

2. Kinds of stream Byte-based streams

Store data in Binary format. Called “binary files”. Data must be converted to a character form before it

is displayed for the user. Character-based streams

Stores the data in Character format. Called “text files”. Also typically has special characters (like \n - which

signifies the end) produced and used by text editors

Different Types of Streams

For Example: Binary Files:

The value 5 is stored in binary format as 101…or more specifically it is stored using an integer format (like 2’s complement) so it would be 000000000000101.

Types: financial files, picture files, program executables etc.

Test Files: The value 5 in character format would be 00000000 00110101 which is the ASCII value for 5 in Unicode format.

Types: text files (.txt), html, xml, etc.

Different Types of Streams

Network Streams

`

Textbook Example: Simple Weather

Text Streams

System.out is a static stream which is automatically open and ready to accept output data:

System.out.print("Enter Date (mm/dd/yyyy): ");

for ( int x= 1; x <= 10; x++) {

date = date + (char) System.in.read();

}

System.in, System.out, System.err. (can be redirected)

Example: Redirection example.

Text Streams

Simple Example - input from the keyboard:

private static String readString() throws IOException

{

String tempS="";

int tempC;

while ((tempC = System.in.read())!='\n')

{

tempS += (char) tempC;

}

return tempS;

}

Example: TextFileMaker1 (example of redirection)

Text Streams

Simple example – input from a file1. Create the input stream object:

BufferedReader input = new BufferedReader(new FileReader(filename));

2. Use readLine

boolean booleanValue = (new Boolean(input.readLine())).booleanValue();

3. Close file

input.close();

See example: TextFileAccessor

Text Streams

Writer

FileWriter PrintWriter

write

write println – independentof the particular stream

PrintWriter file1 = new PrintWriter (new FileWriter(“file.txt” ) );

file1.println(“A line of text”);

Text Streams

read

read readLine – returns nullreference at EOF

BufferedReader file1 = new BufferedReader (new FileReader(“file.txt” ) );

file1.readLine();

Reader

FileReader BufferedReader

Text Streams

Simple example – input from a file1. Create the input stream object:

BufferedReader input = new BufferedReader(new FileReader(filename));

2. Read the linesString curLine = someBufferedReader.readLine();

while(curLine != null) {… curLine = someBufferedReader.readLine();

}3. Close file input.close();

See example: TextFileAccessor

Meta-File Information

What sort of information does a file system contain?

File class - An abstract representation of file and directory pathnames

File name = new File( “filename.dat”);

See examples: FileTest1 & FileTest

Meta-File Information

Methods for class

getName()

isFile()

isDirectory()

isAbsolute()

lastModified()

length()

getPath()

getAbsolutePath()

getParent()

Recommended