Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Similar documents
Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

PIC 20A Streams and I/O

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

CS 251 Intermediate Programming Java I/O Streams

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Darshan Institute of Engineering & Technology for Diploma Studies

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Objec&ves. Review. Standard Error Streams

I/O Streams. Object-oriented programming

CS193j, Stanford Handout #26. Files and Streams

Lecture 11.1 I/O Streams

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系

Java Input/Output. 11 April 2013 OSU CSE 1

Simple Java Input/Output

Exceptions and Working with Files

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Object-Oriented Programming Design. Topic : Streams and Files

Byte and Character Streams. Reading and Writing Console input and output

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming Languages and Techniques (CIS120)

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

HST 952. Computing for Biomedical Scientists Lecture 8

IT101. File Input and Output

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Software 1. Java I/O

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Streams and File I/O

Example: Copying the contents of a file

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

CSC 1214: Object-Oriented Programming

Special error return Constructors do not have a return value What if method uses the full range of the return type?

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CPS122 Lecture: Input-Output

C17: I/O Streams and File I/O

CS112 Lecture: Streams

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

CPS122 Lecture: Input-Output

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

1.00 Lecture 30. Sending information to a Java program

Software Practice 1 - File I/O

Chapter 10. IO Streams

ITI Introduction to Computer Science II

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University

Introduction Unit 4: Input, output and exceptions

Files and Streams

Programming Languages and Techniques (CIS120)

STREAMS. (fluxos) Objetivos

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Programming Languages and Techniques (CIS120)

Principles of Software Construction: Objects, Design and Concurrency. Design Case Study: Stream I/O. toad

Advanced Object-Oriented Programming Streams and Files

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

The I/O Package. THE Java platform includes a number of packages that are concerned with the CHAPTER20

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Principles of Computer Science I

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

Techniques of Java Programming: Streams in Java

Data Structures. 03 Streams & File I/O

Active Learning: Streams

Object Oriented Software Design

Object Oriented Software Design

Java Programming Lecture 9

Programmierpraktikum

Lab 2: File Input and Output

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Files and IO, Streams. JAVA Standard Edition

Core Java Contents. Duration: 25 Hours (1 Month)

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Object-Oriented Programming in the Java language

CSD Univ. of Crete Fall Files, Streams, Filters

Performing input and output operations using a Byte Stream

Chapter 10 Input Output Streams

Transcription:

CSE 143 Overview Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 10/20/2004 (c) 2001-4, University of Washington 12-1 10/20/2004 (c) 2001-4, University of Washington 12-2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING Data Representation Underneath it s all bits (binary digits 0/1) Byte group of 8 binary digits Smallest addressable unit of memory Meaning depends on interpretation Base-10 integers represented as base-2 integers Characters formats include ASCII (1 byte) or Unicode (2 byte) encodings 01000001 = integer 65 = ASCII A Unicode 'A' is 0000000001000001 00111111 = integer 63 = ASCII? 00110110 = integer 54 = ASCII 6 But it s still just bits 10/20/2004 (c) 2001-4, University of Washington 12-3 10/20/2004 (c) 2001-4, University of Washington 12-4 CSE143 Au04 12-1

Representation of Primitive Java Types Boolean 1 byte (0 = false; 1 = true) Integer types byte 1 byte (-128 to 127) short 2 bytes (-32768 to 32767) int 4 bytes (-2147483648 to 2147483647) long 8 bytes (-9223372036854775808 to 9223372036854775807) Floating-point (real number) types float 4 bytes; approx. 6 decimal digits precision, magnitude ~10 ±38 double 8 bytes; approx. 15 decimal digits precision, ~10 ±308 Character type char 2 bytes; Unicode characters w/decimal values 0 to 65535 Unicode International standard Java was first major language to adopt Intended to include all the world s writing systems Characters are 2 bytes (16 bits) Given by Hex digits, e.g. 4EB9 Specifications: www.unicode.org Unicode 3.1 (2001) introduced characters outside the original 16-bit range Not yet well-supported in most languages/systems, but support is defined for Java 10/20/2004 (c) 2001-4, University of Washington 12-5 10/20/2004 (c) 2001-4, University of Washington 12-6 Input and Output Communicating with the outside world Disk (Files) Central Processing Unit Monitor Streams Java model of communication: streams Sequence of data flowing from a source to a program, or from a program to a destination (sink) Files are common sources and sinks Others: network sockets, interprogram communication, Main Memory Keyboard mouse input (source) stream program stream output (sink) Network 10/20/2004 (c) 2001-4, University of Washington 12-7 10/20/2004 (c) 2001-4, University of Washington 12-8 CSE143 Au04 12-2

Stream after Stream... Stream are a useful model for processing data along the way, in a pipeline input file (source) decompressor English- to- French translator encryptor output file (sink) Other Possible Kinds of Stream Converters Compression Encryption Filtering Translation Statistics gathering Security monitoring Routing/Merging Reducing Bandwidth (Size & Detail), e.g. of graphics or sound "lossy compression JPEG, MP3, many others Noise reduction, image sharpening, Many, many more 10/20/2004 (c) 2001-4, University of Washington 12-9 10/20/2004 (c) 2001-4, University of Washington 12-10 Streams vs. Files Many languages don t make clear distinction In Java: file is the collection of data, managed by the operating system stream is a flow of data from one place to another A stream is an abstraction for data flowing from or to a file, remote computer, URL, hardware device, etc. Java Stream Library Huge variety of stream classes in java.io.* Some are data sources or sinks Others are converters that take data from a stream and transform it somehow to produce a stream with different characteristics Highly modular Lots of different implementations all sharing a common interface; can be mixed and matched and chained easily Great OO design example, in principle In practice, it can be very confusing (simple I/O is messy) (improved simple I/O in Java 1.5 printf method, Scanner class) 10/20/2004 (c) 2001-4, University of Washington 12-11 10/20/2004 (c) 2001-4, University of Washington 12-12 CSE143 Au04 12-3

Common Stream Processing Pattern Basic idea the same for input & output // input // output open a stream open a stream while more data { while more data { read & process next data write data to stream close stream close stream Opening & Closing Streams Before a stream can be used it must be opened Create a stream object and connect it to source or destination of the stream data Often done implicitly as part of creating stream objects When we re done with a stream, it should be closed Takes care of any unfinished operations, then breaks the connection between the program and the data source/destination 10/20/2004 (c) 2001-4, University of Washington 12-13 10/20/2004 (c) 2001-4, University of Washington 12-14 Java Streams Character Input Streams 2 major families of stream classes Byte streams read/write byte values Corresponds to physical data network and disk I/O streams Abstract classes: InputStream and OutputStream Character streams read/write char values Added in Java 1.1 Primary (Unicode) text input/output stream classes Abstract classes: Reader and Writer Footnote: System.in and System.out should be character streams, but are byte streams for historical reasons (existed before Java 1.1, when character streams were added, and remain unchanged to preserve backward compatibility) 10/20/2004 (c) 2001-4, University of Washington 12-15 10/20/2004 (c) 2001-4, University of Washington 12-16 CSE143 Au04 12-4

Byte Output Streams Streams and Exceptions Many operations can throw IOException All input operations, in particular Normally throws a specific subclass of IOException depending on the actual error IOException is checked (Review question: what does a checked exception imply?) 10/20/2004 (c) 2001-4, University of Washington 12-17 10/20/2004 (c) 2001-4, University of Washington 12-18 Reader int read( ); Basic Reader/Writer Operations int read(char[ ] cbuf); void close( ); // return Unicode value of next character; // return -1 if end-of-stream // read several characters into array; // return -1 if end-of-stream // close the stream Writer void write(int c); // write character whose Unicode value is c void write(char[ ] cbuf);// write array contents void write(string s); // write string void close( ); // close the stream To convert Unicode int to char, or vice versa: use a cast File Readers and Writers To read a (Unicode) text file (not a binary data file), instantiate FileReader A subclass of Reader: implements read and close operations Constructors take a File object or a string with the name of the file to open and read from To write to a text file, instantiate FileWriter A subclass of Writer: implements write and close operations Constructors take a File object or the name of the file to open/create and overwrite (can also append to an existing file using a different constructor) 10/20/2004 (c) 2001-4, University of Washington 12-19 10/20/2004 (c) 2001-4, University of Washington 12-20 CSE143 Au04 12-5

Text Files vs Char Data Most of the world s text files use 8-bit characters ASCII and variations of ASCII Internal to Java, char data is always 2-byte Unicode Java Reader deals only with Unicode Big problem: how to read and write normal (ASCII) text files in Java? Solution: stream classes which adapts 8-bit chars to Unicode Generally taken care of automatically normally don t need to worry about the distinction Copy a Text File, One Character at a Time public void copyfile(string sourcefilename, String destfilename) throws IOException { FileReader infile = new FileReader(sourceFilename); FileWriter outfile = new FileWriter(destFilename); int ch = infile.read( ); while (ch!= -1) { outfile.write(ch); System.out.println("The next char is \'" + (char)ch + "\'"); // why \'? ch = infile.read( ); infile.close( ); outfile.close( ); 10/20/2004 (c) 2001-4, University of Washington 12-21 10/20/2004 (c) 2001-4, University of Washington 12-22 Interlude: Where is the File? In the previous slide, we opened the files with FileReader infile = new FileReader(sourceFilename); FileWriter outfile = new FileWriter(destFilename); The file names could be complete paths like c:\documents and Settings\J User\story.txt, but Not portable different operating systems have different file naming conventions Not convenient what if we move the document? Would like to be able to use a name like story.txt to open the file But if we do, where should we put the file? 10/20/2004 (c) 2001-4, University of Washington 12-23 File Directories When we use a simple file name FileReader infile = new FileReader( story.txt ); Java looks for that file in the current directory Current directory If the program is executed from a command-line prompt, it is the current directory when the java command is entered If it is executed by DrJava, BlueJ, or other development tools, it likely is not Is there a portable scheme way to find the file, assuming it s in the same directory or jar file as the main program.class file? Yes but you might not really want to have to know the details 10/20/2004 (c) 2001-4, University of Washington 12-24 CSE143 Au04 12-6

Finding Files (optional) The industrial-strength solution is to use a class loader method that will search all directories it knows about Includes the directory or jar file containing the program s.class files, Java standard libraries, any additional libraries on the classpath, etc. Ready? URL url = getclass().getclassloader().getresource(filename); If url!=null, then it can be used to open the file (also works for other resources like images and icons) Credit: Found on bluej.org; see their tip #10 for more details No, this won t be on the test Opening Files Using File Dialogs Easy, portable solution for our purposes is JFileDialog Lots (tons) of options, but basic use is quite simple JFileChooser chooser = new JFileChooser(); int result1 = chooser.showopendialog(null); File infile = chooser.getselectedfile(); System.out.println("Input file selected is " + infile); int result2 = chooser.showsavedialog(null); File outfile = chooser.getselectedfile(); System.out.println("Output file selected is " + outfile); The int results of the show Dialog methods indicate whether the dialog was dismissed with ok, cancel, or something else Should really check this before getting the selected file info 10/20/2004 (c) 2001-4, University of Washington 12-25 10/20/2004 (c) 2001-4, University of Washington 12-26 More Efficient I/O BufferedReader/Writer Can improve efficiency by reading/writing many characters at a time BufferedReader: a converter stream that performs this chunking BufferedReader constructor takes any kind of Reader as an argument -- can make any read stream buffered BufferedReader supports standard Reader operations -- clients don't have to change to benefit from buffering Key addition: provides a portable readline( ) String readline( ); // return an entire line of input; or null if // end-of-stream reached [handles the complexities of how end-of-line is represented on different systems] BufferedWriter BufferedWriter: a converter stream that performs chunking on writes BufferedWriter constructor takes any kind of Writer as an argument BufferedWriter supports standard Writer operations Also supports newline( ) void newline( ); // write an end-of-line character [As with readline, does the appropriate thing for the local system s convention for how end-of-line is actually represented] 10/20/2004 (c) 2001-4, University of Washington 12-27 10/20/2004 (c) 2001-4, University of Washington 12-28 CSE143 Au04 12-7

Copy a Text File, One Line at a Time public void copyfile(string sourcefilename, String destfilename) throws IOException { BufferedReader infile = new BufferedReader(new FileReader(sourceFilename)); BufferedWriter outfile = new BufferedWriter(new FileWriter(destFilename)); String line = infile.readline( ); while (line!= null) { outfile.write(line); outfile.newline( ); System.out.println("The next line is \"" + line + "\""); line = infile.readline( ); infile.close( ); outfile.close( ); PrintWriter PrintWriter is another converter for a write stream Adds print & println methods for primitive types, strings, objects, etc., just as we've used for System.out Does not throw exceptions (to make it more convenient to use) Optional 2 nd boolean parameter in constructor to request output be flushed (force all output to actually appear) after each println Useful for interactive consoles where messages need to appear right away 10/20/2004 (c) 2001-4, University of Washington 12-29 10/20/2004 (c) 2001-4, University of Washington 12-30 Copy a Text File, Using PrintWriter public void copyfile(string srcfilename, String destfilename) throws IOException { BufferedReader infile = new BufferedReader(new FileReader(srcFilename)); PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(destFilename))); String line = infile.readline( ); while (line!= null) { outfile.println(line); System.out.println("The next line is \"" + line + "\""); line = infile.readline( ); infile.close( ); outfile.close( ); StringReader and StringWriter Strings as streams(!) StringReader: construct character stream from a String StringReader instream = new StringReader("the source"); // could now copy instream to a file, or somewhere else StringWriter: write stream to a String StringWriter outstream = new StringWriter( ); // now write onto outstream, using outstream.write( ), outstream.print( ), etc. String theresult = outstream.tostring( ); 10/20/2004 (c) 2001-4, University of Washington 12-31 10/20/2004 (c) 2001-4, University of Washington 12-32 CSE143 Au04 12-8

Binary Streams For processing binary data (encoded characters, executable programs, other low-level data), use InputStreams and OutputStreams Operations are similar to Reader and Writer operations Replace char with byte in read; no write(string) Many analogous classes to Readers and Writers: FileInputStream, FileOutputStream BufferedInputStream, BufferedOutputStream ByteArrayInputStream, ByteArrayOuputStream ObjectInputStream, ObjectOutputStream -- read & write whole objects! Conversion from Binary to Text Streams InputStreamReader: creates a Reader from an InputStream // System.in is of type InputStream Reader instream = new InputStreamReader(System.in); // now can treat it nicely as a character stream OutputStreamWriter: creates a Writer from an OutputStream // System.out is of type OutputStream Writer outstream = new OutputStreamWriter(System.out); // now can treat it nicely as a character stream 10/20/2004 (c) 2001-4, University of Washington 12-33 10/20/2004 (c) 2001-4, University of Washington 12-34 Network Streams Import java.net.* Use URL to create a name of something on the web Use openstream() method to get a InputStream on the contents of the URL URL url = new URL("http://www.cs.washington.edu/index.html"); InputStream instream = url.openstream( ); // now read from instream Use openconnection( ) and URLConnection methods to get more control URLConnection connection = url.openconnection( ); OutputStream outstream = connection.getoutputstream( ); // now write to outstream (assuming target url allows writing!) Socket class for even more flexible network reading & writing But lower-level; program has to take care of more details 10/20/2004 (c) 2001-4, University of Washington 12-35 Summary Java stream libraries Comprehensive, flexible, easy to compose multiple streams in a chain But not simple to do simple things What to take away BufferedReader and readline( ) for text input PrintWriter and print( )/println( ) for text output JFileChooser to select files when opening close( ) when done The rest should give you pointers to things to learn when you need them 10/20/2004 (c) 2001-4, University of Washington 12-36 CSE143 Au04 12-9