CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
File Handling and Serialization CSIS 3701: Advanced Object Oriented Programming.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Array Yoshi. Definition An array is a container object that holds a fixed number of values of a single type. The length of an array is established when.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Java I/O and Java Networking (Client Side) Yoshi.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
ARRAYS public class ArrayDemo { public static void main(String[] args) { int[] anArray; // declare an array of integers anArray = new int[10]; // create.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
CSC – Java Programming II Lecture 9 January 30, 2002.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Two Ways to Store Data in a File Text format Binary format.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Files Chap. 10 Streams, Readers, Writers 1. 2 Problem In our array example, we entered the students' names and scores from the keyboard. In many situations.
File IO Basics By Dan Fleck Coming up: Data Streams.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
Two Ways to Store Data in a File  Text format  Binary format.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Array and String.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
The Java IO System Different kinds of IO Different kinds of operations
IO in java.
IO in java.
Exception Handling, Reading and Writing in Files, Serialization,
Strings and File I/O.
12: The Java I/O System stream model.
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
Web Design & Development Lecture 8
Java IO Packages Prepared by Mrs.S.Amudha AP/SWE
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
David Davenport Spring 2005
Presentation transcript:

CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Arrays – One-Dimensional – Two-Dimensional – Multi-Dimensional Stream I/O – Text-file I/O – Binary-file I/O

CS-2852 Data Structures, Andrew J. Wozniewicz Array Definition Fixed number of values of a single type arranged consecutively and accessed by index.

CS-2852 Data Structures, Andrew J. Wozniewicz Array Declarations int[] anArrayOfInts; byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings;

CS-2852 Data Structures, Andrew J. Wozniewicz One-Dimensional Array

CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dimensional Arrays A multidimensional array is simply an array whose components are themselves arrays. The rows are allowed to vary in length. Arrays of Arrays of…

CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dim Array Declaration int[][] a2; int[][] a2 = new int[10][5]; String[][] Data; byte[][] screenPix; byte[][] raggedArray = new byte[5][];

CS-2852 Data Structures, Andrew J. Wozniewicz Multi-Dim Array Example class MultiDimArrayDemo { public static void main(String[] args) { String[][] names = {{"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"}}; System.out.println(names[0][0] + names[1][0]); //Mr. Smith System.out.println(names[0][2] + names[1][1]); //Ms. Jones }

CS-2852 Data Structures, Andrew J. Wozniewicz Standard I/O Streams System.in System.out System.err Opened automatically and available without any further action.

CS-2852 Data Structures, Andrew J. Wozniewicz Standard I/O Streams Byte streams instead of character streams: – System.in PrintStream (is-a java.io.OutputStream, is-a java.io.FilterOutputStream): – System.out – System.err

CS-2852 Data Structures, Andrew J. Wozniewicz Using System.in import java.io.*; public class StreamIOTest1 { public static void main(String args[]) throws IOException { System.out.println("Hi!"); System.out.print("Enter your name: "); InputStreamReader cin = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(cin); String name = input.readLine(); System.out.print("Hello "); System.out.println(name); }

CS-2852 Data Structures, Andrew J. Wozniewicz Using Console import java.io.*; public class ConsoleIOTest1 { public static void main(String[] args) { Console c = System.console(); if (c == null) { System.err.println("No console."); System.exit(1); } String login = c.readLine("Enter your user name: "); c.printf("Welcome, %1$s.\n", login); }

CS-2852 Data Structures, Andrew J. Wozniewicz Reading Text Files import java.io.*; public class TextFileReadDemo { public static void main(String[] args) throws IOException { String inFileName = args[0]; BufferedReader ins = new BufferedReader( new FileReader(inFileName) ); do { String line = ins.readLine(); if (line == null) break; System.out.println(line); } while (true); }

CS-2852 Data Structures, Andrew J. Wozniewicz Writing Text Files import java.io.*; public class TextFileWriteDemo { public static void main(String[] args) throws IOException { String outFileName = args[0]; String[] names = new String[] {"Andrew","Dorota","Martin", "Anna","Mimi","Kiki"}; PrintWriter outs = new PrintWriter(new FileWriter(outFileName)); for (int i = 0; i < names.length; i++) { outs.println(names[i]); } outs.close(); }

CS-2852 Data Structures, Andrew J. Wozniewicz Writing Binary Files – Part I import java.io.*; public class BinaryDataWriterOnlyTest1 { static final String dataFile = "invoicedata.dat"; static final double[] prices = { 2.99, 4.79, 5.99, 3.99, 5.99 }; static final int[] units = { 12, 8, 13, 10, 12 }; static final String[] descriptions = { "Milk", "Cereal", "Potato Chips", "Salsa", "Cheddar Cheese" };... }

CS-2852 Data Structures, Andrew J. Wozniewicz Writing Binary Files – Part II public static void main(String[] args) throws IOException { DataOutputStream out = null; try { out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(dataFile))); for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeInt(units[i]); out.writeUTF(descriptions[i]); } } finally { out.close(); }

CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part I import java.io.*; public class BinaryDataReadOnlyTest1 { static final String dataFile = "invoicedata.dat"; public static void main(String[] args) throws IOException { DataInputStream in = null; }

CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part II double total = 0.0; try { in = new DataInputStream( new BufferedInputStream( new FileInputStream(dataFile))); double price; int unit; String desc; finally { in.close(); }

CS-2852 Data Structures, Andrew J. Wozniewicz Reading Binary Files – Part II try { while (true) { price = in.readDouble(); unit = in.readInt(); desc = in.readUTF(); System.out.format( "You ordered %d units of %s at $%.2f%n", unit, desc, price); total += unit * price; } } catch (EOFException e) { } System.out.format("For a TOTAL of: $%.2f%n", total);

CS-2852 Data Structures, Andrew J. Wozniewicz Summary Arrays – fixed number of values of a single type – one-, or multi-dimensional Stream I/O – Simple console I/O: System.in, System.out – Text-file I/O: BufferedReader, FileReader PrintWriter, FileWriter – Binary-file I/O DataInputStream, BufferedInputStream, FileInputStream DataOutputStream, BufferedOutputStream, FileOutputStream

Questions? Image copyright © 2010 andyjphoto.com