Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Methodology File Input

Similar presentations


Presentation on theme: "Computer Programming Methodology File Input"— Presentation transcript:

1 Computer Programming Methodology File Input
COS 130 Computer Programming Methodology File Input

2 Reading Binary Data Scanner class only reads text data – letters
The ppm graphics files use binary data to save space. Therefore we will need some other way to read files. DataInputStream class will allow us to read binary data

3 DataInputStream This finds the library This makes some else
import java.io.*; class myClass { public static void main (String args[]) throws IOException { DataInputStream reader = new DataInputStream(System.in); int id1 = reader.readUnsignedByte(); if (id1 == 'P') { //found a capital P // stuff } This makes some else deal with our problems Creates a DataInputStream Object This specifies the keyboard Or a redirected file This reads a byte

4 Convert char to int In the P6 file the width and height are stored as characters (letters) However, we will need to do math with them Specifically we will want to use them to decide how many pixels to read in.

5 Example convert char to int
int number = 0; int datum = reader.readUnsignedByte(); while(Character.isWhitespace(datum)) { datum = reader.readUnsignedByte(); } while (Character.isDigit(datum)) { number= number * 10 + Character.getNumericValue(datum);

6 Casting Putting Big Things into Little Things
Java attempts to protect you Example: double pi = 3.14; double r = 7; int result = pi * r * r; Will Not Compile! Putting a double (which is big) into an int will lose things

7 Casting Trust Me! I know what I am doing! double pi = 3.14;
double r = 7; int result =(int) (pi * r * r); Cast operator Trust Me! I know what I am doing!

8 Example Make a color 10% brighter Will result in a double
Giving a compile error red = red * ; (int) ( )


Download ppt "Computer Programming Methodology File Input"

Similar presentations


Ads by Google