CPIT 305 Advanced Programming

Size: px
Start display at page:

Download "CPIT 305 Advanced Programming"

Transcription

1 M. G. Abbas Malik CPIT 305 Advanced Programming Assistant Professor Faculty of Computing and IT University of Jeddah

2 Advanced Programming Course book: Introduction to Java Programming: Comprehensive Edition, 10th Edition, Y. Daniel Liang, Pearson Reference BooK: Java How to Program, 9th Edition, Paul Deitel and Harvey Deitel, Deitel. Discrete Mathematics and its Application, Kenneth H. Rosen, 7th Edition, McGraw-Hill Java Docs: Course Home: 75% attendance is compulsory to sit in the final exam M. G. Abbas Malik, FCIT, UoJ 2

3 Advanced Programming Topics Covered Recursion Revisited Review of Object Oriented Concepts Exception Handling File and Stream Handling Binary I/O Multithreading and Parallel Programming Networking - Socket Programming Java Database Programming M. G. Abbas Malik, FCIT, UoJ 3

4 File, Streams and Chapters: 17 Binary I/O M. G. Abbas Malik, FCIT, UoJ 4

5 File Basics Recall that a file is block structured. What does this mean? What happens when an application opens or closes a file? Every OS has its own EOF character and, for text files, its own EOL character(s). M. G. Abbas Malik, FCIT, UoJ 5

6 Streams Java file I/O involves streams. You write and read data to streams. The purpose of the stream abstraction is to keep program code independent from physical devices. Three stream objects are automatically created for every application: System.in, System.out, and System.err. M. G. Abbas Malik, FCIT, UoJ 6

7 Types of Streams There are 2 kinds of streams byte streams character streams M. G. Abbas Malik, FCIT, UoJ 7

8 Character Streams Character streams create text files. These are files designed to be read with a text editor. Java automatically converts its internal unicode characters to the local machine representation (ASCII). M. G. Abbas Malik, FCIT, UoJ 8

9 Byte Streams Byte streams create binary files. A binary file essentially contains the memory image of the data. That is, it stores bits as they are in memory. Binary files are faster to read and write because no translation need take place. Binary files, however, cannot be read with a text editor. M. G. Abbas Malik, FCIT, UoJ 9

10 I/O Classes Java has 6 classes to support stream I/O File: An object of this class is either a file or a directory. OutputStream: base class for byte output streams InputStream: base class for byte input streams M. G. Abbas Malik, FCIT, UoJ 10

11 I/O Classes Writer: base class for character output streams. Reader: base class for character input streams. RandomAccessFile: provides support for random access to a file. Note that the classes InputStream, OutputStream, Reader, and Writer are abstract classes. M. G. Abbas Malik, FCIT, UoJ 11

12 Text I/O in Java File class Scanner class PrintWriter class M. G. Abbas Malik, FCIT, UoJ 12

13 I/O Streams M. G. Abbas Malik, FCIT, UoJ 13

14 Text Stream Vs. Binary Stream M. G. Abbas Malik, FCIT, UoJ 14

15 Text Stream Vs. Binary Stream Text I/O requires encoding and decoding Binary I/O does not involve encoding or decoding, thus is more efficient than text I/O M. G. Abbas Malik, FCIT, UoJ 15

16 Binary I/O Classes The abstract InputStream is the root class for reading binary data, and the abstract OutputStream is the root class for writing binary data M. G. Abbas Malik, FCIT, UoJ 16

17 Binary I/O java.io.inputstream Abstract Class InputStream is the root for binary input classes M. G. Abbas Malik, FCIT, UoJ 17

18 Binary I/O java.io.outputstream Abstract Class OutputStream is the root for binary output classes M. G. Abbas Malik, FCIT, UoJ 18

19 Binary I/O java.io.fileinputstream This class inherits all method of InputStream class, root class for FileInputStream M. G. Abbas Malik, FCIT, UoJ 19

20 Binary I/O java.io.fileoutputstream This class inherits all method of OutputStream class, root class for FileOutputStream M. G. Abbas Malik, FCIT, UoJ 20

21 Binary I/O Using I/O Methods Almost all the methods in the I/O classes throw java.io.ioexception M. G. Abbas Malik, FCIT, UoJ 21

22 FileStream Example LISTING 17.1 TestFileStream.java 1 import java.io.*; 2 3 public class TestFileStream { 4 public static void main(string[] args) throws IOException { 5 try ( 6 // Create an output stream to the file as TRY-With-Resource 7 FileOutputStream output = new FileOutputStream("temp.dat"); 8 ){ 9 // Output values to the file 10 for (int i = 1; i <= 10; i++) 11 output.write(i); 12 } try ( 15 // Create an input stream for the file as TRY-With-Resource 16 FileInputStream input = new FileInputStream("temp.dat"); 17 ) { 18 // Read values from the file 19 int value; 20 while ((value = input.read())!= -1) 21 System.out.print(value + " "); 22 } 23 } 24} Creating the FileOutStream such that it automatically closed when TRY block ends. Creating the FileInStream such that it automatically closed when TRY block ends. M. G. Abbas Malik, FCIT, UoJ 22

23 File Streams When a stream is no longer needed, always close it using the close() method or automatically close it using a try-with-resource statement Not closing streams may cause data corruption in the output file, or other programming errors M. G. Abbas Malik, FCIT, UoJ 23

24 File Streams The root directory for the file is the classpath directory. For the example in this book, the root directory is c:\book, so the file temp.dat is located at c:\book. If you wish to place temp.dat in a specific directory FileOutputStream output = new FileOutputStream ("directory/temp.dat"); File temp.dat will be stored in the folder directory M. G. Abbas Malik, FCIT, UoJ 24

25 File Streams An instance of FileInputStream can be used as an argument to construct a Scanner An instance of FileOutputStream can be used as an argument to construct a PrintWriter new PrintWriter(new FileOutputStream("temp.txt", true)); If temp.txt does not exist, it is created. If temp.txt already exists, new data are appended to the file. M. G. Abbas Malik, FCIT, UoJ 25

26 File Streams Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can be used only for reading bytes The basic byte output stream provides a write method that can be used only for writing bytes If we want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream M. G. Abbas Malik, FCIT, UoJ 26

27 DataInputStream DataInputStream reads bytes from the stream and converts them into appropriate primitive-type values or strings DataInputStream extends FilterInputStream and implements the DataInput interface M. G. Abbas Malik, FCIT, UoJ 27

28 DataInputStream M. G. Abbas Malik, FCIT, UoJ 28

29 DataOutputStream DataOutputStream converts primitive-type values or strings into bytes and outputs the bytes to the stream DataOutputStream extends FilterOutputStream and implements the DataOutput interface M. G. Abbas Malik, FCIT, UoJ 29

30 DataOutputStream M. G. Abbas Malik, FCIT, UoJ 30

31 Data Stream Example LISTING 17.2 TestDataStream.java 1 import java.io.*; 2 3 public class TestDataStream { Writing Data in the File temp.dat 4 public static void main(string[] args) throws IOException { 5 try ( // Create an output stream for file temp.dat 6 DataOutputStream output = 7 new DataOutputStream(new FileOutputStream("temp.dat")); 8 ) { 9 // Write student test scores to the file 10 output.writeutf("john"); 11 output.writedouble(85.5); 12 output.writeutf("jim"); 13 output.writedouble(185.5); 14 output.writeutf("george"); 15 output.writedouble(105.25); 16 } Reading Data from the File temp.dat try ( // Create an input stream for file temp.dat 19 DataInputStream input = 20 new DataInputStream(new FileInputStream("temp.dat")); 21 ) { 22 // Read student test scores from the file 23 System.out.println(input.readUTF() + " " + input.readdouble()); 24 System.out.println(input.readUTF() + " " + input.readdouble()); 25 System.out.println(input.readUTF() + " " + input.readdouble()); 26 } 27 } 28} M. G. Abbas Malik, FCIT, UoJ 31

32 Data Stream Example M. G. Abbas Malik, FCIT, UoJ 32

33 End-of-File If you keep reading data at the end of an InputStream, an EOFException will occur This EOFException exception can be used to detect the end of a file M. G. Abbas Malik, FCIT, UoJ 33

34 End-of-File LISTING 17.3 DetectEndOfFile.java 1 import java.io.*; 2 3 public class DetectEndOfFile { 4 public static void main(string[] args) { 5 try { 6 try (DataOutputStream output = 7 new DataOutputStream(new FileOutputStream("test.dat"))) { 8 output.writedouble(4.5); 9 output.writedouble(43.25); 10 output.writedouble(3.2); 11 } try (DataInputStream input = 14 new DataInputStream(new FileInputStream("test.dat"))) { 15 while (true) 16 System.out.println(input.readDouble()); 17 } 18 } 19 catch (EOFException ex) { 20 System.out.println("All data were read"); 21 } 22 catch (IOException ex) { 23 ex.printstacktrace(); 24 } 25 } 26 } Writing Data in the File test.dat Reading Data from the File test.dat Detecting EoF in the File test.dat M. G. Abbas Malik, FCIT, UoJ 34

35 Buffered Input/Output Streams BufferedInputStream/BufferedOutputStream can be used to speed up input and output by reducing the number of disk reads and writes Using BufferedInputStream, the whole block of data on the disk is read into the buffer in the memory once. The individual data are then delivered to your program from the buffer M. G. Abbas Malik, FCIT, UoJ 35

36 Buffered Input/Output Streams M. G. Abbas Malik, FCIT, UoJ 36

37 BufferedInputStream No New Methods manages a buffer behind the scene and automatically reads/writes data from/to disk on demand If no buffer size is specified, the default size is 512 bytes M. G. Abbas Malik, FCIT, UoJ 37

38 BufferedOutputStream No New Methods manages a buffer behind the scene and automatically reads/writes data from/to disk on demand M. G. Abbas Malik, FCIT, UoJ 38

39 Case Study: Copying Files Self Study Chapter 17: Section 17.5 M. G. Abbas Malik, FCIT, UoJ 39

40 Object I/O ObjectInputStream/ObjectOutputStream classes can be used to read/write serializable objects DataInputStream/DataOutputStream enables you to perform I/O for primitive-type values and strings ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition to primitive-type values and strings M. G. Abbas Malik, FCIT, UoJ 40

41 Object I/O ObjectInputStream/ObjectOutputStream contains all the functions of DataInputStream/ DataOutputStream, we can replace DataInputStream/DataOutputStream completely with ObjectInputStream/ObjectOutputStream M. G. Abbas Malik, FCIT, UoJ 41

42 ObjectInputStream ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants M. G. Abbas Malik, FCIT, UoJ 42

43 ObjectOutputStream ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants M. G. Abbas Malik, FCIT, UoJ 43

44 ObjectOutputStream Example LISTING 17.5 TestObjectOutputStream.java 1 import java.io.*; 2 3 public class TestObjectOutputStream { 4 public static void main(string[] args) throws IOException { 5 try ( // Create an output stream for file object.dat 6 ObjectOutputStream output = 7 new ObjectOutputStream(new FileOutputStream("object.dat")); 8 ) { 9 // Write a string, double value, and object to the file 10 output.writeutf("john"); 11 output.writedouble(85.5); 12 output.writeobject(new java.util.date()); 13 } 14 } 15} M. G. Abbas Malik, FCIT, UoJ 44

45 ObjectInputStream Example LISTING 17.6 TestObjectInputStream.java 1 import java.io.*; 2 3 public class TestObjectInputStream { 4 public static void main(string[] args) 5 throws ClassNotFoundException, IOException { 6 try ( // Create an input stream for file object.dat 7 ObjectInputStream input = 8 new ObjectInputStream(new FileInputStream("object.dat")); 9 ) { 10 // Read a string, double value, and object from the file 11 String name = input.readutf(); 12 double score = input.readdouble(); 13 java.util.date date = (java.util.date)(input.readobject()); 14 System.out.println(name + " " + score + " " + date); 15 } 16 } 17} M. G. Abbas Malik, FCIT, UoJ 45

46 Serializable Interface Not every object can be written to an output stream Objects that can be so written are said to be serialisable A serializable object is an instance of the java.io.serializable interface, so the object s class must implement Serializable The Serializable interface is a marker interface Implementing this interface enables the Java serialization mechanism to automate the process of storing objects and arrays M. G. Abbas Malik, FCIT, UoJ 46

47 What s an Array List ArrayList is a class in the standard Java libraries that can hold any type of object an object that can grow and shrink while your program is running (unlike arrays, which have a fixed length once they have been created) In general, an ArrayList serves the same purpose as an array, except that an ArrayList can change length while the program is running ArrayList Slides are adopted from M. G. Abbas Malik, FCIT, UoJ 47

48 The ArrayList Class The class ArrayList is implemented using an array as a private instance variable When this hidden array is full, a new larger hidden array is created and the data is transferred to this new array M. G. Abbas Malik, FCIT, UoJ 48

49 Using the ArrayList Class In order to make use of the ArrayList class, it must first be imported import java.util.arraylist; An ArrayList is created and named in the same way as object of any class, except that you specify the base type as follows: ArrayList<BaseType> alist = n e w A r r a y L i s t < B a s e T y p e > ( ) ; M. G. Abbas Malik, FCIT, UoJ 49

50 Creating an ArrayList An initial capacity can be specified when creating an ArrayList as well The following code creates an ArrayList that stores objects of the base type String with an initial capacity of 20 items ArrayList<String> list = new ArrayList<String>(20); Specifying an initial capacity does not limit the size to which an ArrayList can eventually grow Note that the base type of an ArrayList is specified as a type parameter M. G. Abbas Malik, FCIT, UoJ 50

51 Adding elements to an ArrayList The add method is used to add an element at the end of an ArrayList list.add("something"); The method name add is overloaded There is also a two argument version that allows an item to be added at any currently used index position or at the first unused position M. G. Abbas Malik, FCIT, UoJ 51

52 How many elements? The size method is used to find out how many indices already have elements in the ArrayList int howmany = list.size(); The set method is used to replace any existing element, and the get method is used to access the value of any existing element list.set(index, "something else"); String thing = list.get(index); size is NOT capacity size is the number of elements currently stored in the ArrayList Capacity is the maximum number of elements which can be stored. Capacity will automatically increase as needed M. G. Abbas Malik, FCIT, UoJ 52

53 ArrayList code Example // Note the use of Integer, rather than int public static void main( String[ ] args) { ArrayList<Integer> myints = new ArrayList<Integer>(25); System.out.println( Size of myints = + myints.size()); for (int k = 0; k < 10; k++) myints.add( 3 * k ); myints.set( 6, 44 ); System.out.println( Size of myints = + myints.size()); for (int k = 0; k < myints.size(); k++) System.out.print( myints.get( k ) +, ); } Size of myints = 0 Size of myints = 10 0, 3, 6, 9, 12, 15, 44, 21, 24, 27 M. G. Abbas Malik, FCIT, UoJ 53

54 Methods in the Class ArrayList The tools for manipulating arrays consist only of the square brackets and the instance variable length ArrayLists, however, come with a selection of powerful methods that can do many of the things for which code would have to be written in order to do them using arrays M. G. Abbas Malik, FCIT, UoJ 54

55 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 55

56 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 56

57 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 57

58 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 58

59 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 59

60 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 60

61 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 61

62 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 62

63 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 63

64 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 64

65 Some Methods in the Class ArrayList M. G. Abbas Malik, FCIT, UoJ 65

66 More example code // Note the use of Integer instead of int public static void main( String[ ] args) { ArrayList<Integer> myints = new ArrayList<Integer>(25); System.out.println( Size of myints = + myints.size()); for (int k = 0; k < 10; k++) myints.add( 3 * k ); myints.set( 6, 44 ); myints.add( 4, 42 ); myints.remove( new Integer(99) ); System.out.println( Size of myints = + myints.size()); for (int k = 0; k < myints.size(); k++) System.out.print( myints.get( k ) +, ); if (myints.contains( 57 ) ) System.out.println( 57 found ); System.out.println ( 44 found at index + myints.indexof(44)); } M. G. Abbas Malik, FCIT, UoJ 66

67 Why are Some Parameters of Type Base_Type and Others of type Object When looking at the methods available in the ArrayList class, there appears to be some inconsistency In some cases, when a parameter is naturally an object of the base type, the parameter type is the base type However, in other cases, it is the type Object This is because the ArrayList class implements a number of interfaces, and inherits methods from various ancestor classes These interfaces and ancestor classes specify that certain parameters have type Object M. G. Abbas Malik, FCIT, UoJ 67

68 The "For Each" Loop The ArrayList class is an example of a collection class Starting with version 5.0, Java has added a new kind of for loop called a for-each or enhanced for loop This kind of loop has been designed to cycle through all the elements in a collection (like an ArrayList) M. G. Abbas Malik, FCIT, UoJ 68

69 for-each example public class ForEach { public static void main(string[ ] args) { ArrayList<Integer> list = new ArrayList<Integer>; list.add( 42 ); list.add( 57 ); list.add( 86 ); // for each Integer, i, in list for( Integer i : list ) System.out.println( i ); } } //-- Output M. G. Abbas Malik, FCIT, UoJ 69

70 Copying an ArrayList // create an ArrayList of Integers ArrayList<Integer> a = new ArrayList<Integer>( ); a.add(42); a.add(57); a.add(86); Assignment doesn t work As we ve seen with any object, using assignment just makes two variables refer to the same ArrayList. ArrayList<Integer> b = a; a b Stack Heap M. G. Abbas Malik, FCIT, UoJ 70

71 Copying an ArrayList ArrayList s clone( ) method makes a shallow copy ArrayList<Integer> b = a.clone( ); a b Stack Heap M. G. Abbas Malik, FCIT, UoJ 71

72 Copying an ArrayList We need to manually make a deep copy ArrayList<Integer> b = a.clone( ); for( int k = 0; k < b.size( ); k++) b.set(k, a.get(k)); a b Stack Heap M. G. Abbas Malik, FCIT, UoJ 72

73 ArrayList vs Array Why use an array instead of an ArrayList 1. An ArrayList is less efficient than an array 2. ArrayList does not have the convenient square bracket notation 3. The base type of an ArrayList must be a class type (or other reference type). It cannot be a primitive type. (Although wrappers, auto boxing, and auto unboxing make this a non-issue with Java 5) M. G. Abbas Malik, FCIT, UoJ 73

74 ArrayList vs Array Why use an ArrayList instead of an array? 1. Arrays can t grow. Their size is fixed at compile time. ArrayList grows and shrinks as needed while your program is running 2. You need to keep track of the actual number of elements in your array (recall partially filled arrays). ArrayList will do that for you. 3. Arrays have no methods (just length instance variable) ArrayList has powerful methods for manipulating the objects within it M. G. Abbas Malik, FCIT, UoJ 74

75 The Vector Class The Java standard libraries have a class named Vector that behaves almost exactly the same as the class ArrayList In most situations, either class could be used, however the ArrayList class is newer (Java 5), and is becoming the preferred class M. G. Abbas Malik, FCIT, UoJ 75

76 Serializable Objects Suppose you wish to store an ArrayList object To do this you need to store all the elements in the list Each element is an object that may contain other objects Fortunately, you don t have to go through it manually M. G. Abbas Malik, FCIT, UoJ 76

77 Serializable Objects Java provides a built-in mechanism to automate the process of writing objects Process is referred as object serialization, which is implemented in ObjectOutputStream In contrast, the process of reading objects is referred as object deserialization, which is implemented in ObjectInputStream M. G. Abbas Malik, FCIT, UoJ 77

78 Serializable Objects Many classes in the Java API implement Serializable All the wrapper classes for primitive type values, java.math.biginteger, java.math.bigdecimal, java.lang.string, java.lang.stringbuilder, java.lang.stringbuffer, java.util.date, and java.util.arraylist implement java.io.serializable Attempting to store an object that does not support the Serializable interface would cause a NotSerializableException M. G. Abbas Malik, FCIT, UoJ 78

79 Serializing Arrays An array is serializable if all its elements are serializable An entire array can be saved into a file using writeobject and later can be restored using readobject M. G. Abbas Malik, FCIT, UoJ 79

80 Serializing Arrays LISTING 17.7 TestObjectStreamForArray.java 1 import java.io.*; 2 public class TestObjectStreamForArray { 3 public static void main(string[] args) 4 throws ClassNotFoundException, IOException { 5 int[] numbers = {1, 2, 3, 4, 5}; 6 String[] strings = {"John", "Susan", "Kim"}; 7 try ( // Create an output stream for file array.dat 8 ObjectOutputStream output = new ObjectOutputStream(new 9 FileOutputStream("array.dat", true)); 10 ) { 11 // Write arrays to the object output stream 12 output.writeobject(numbers); 13 output.writeobject(strings); 14 } 15 try ( // Create an input stream for file array.dat 16 ObjectInputStream input = 17 new ObjectInputStream(new FileInputStream("array.dat")); 18 ) { 19 int[] newnumbers = (int[])(input.readobject()); 20 String[] newstrings = (String[])(input.readObject()); 21 // Display arrays 22 for (int i = 0; i < newnumbers.length; i++) 23 System.out.print(newNumbers[i] + " "); 24 System.out.println(); 25 for (int i = 0; i < newstrings.length; i++) 26 System.out.print(newStrings[i] + " "); 27 } 28 } 29} M. G. Abbas Malik, FCIT, UoJ 80

81 Random Access Files All of thestreams you have used so far are known as read-only or write-only streams. These streams are called sequential streams A file that is opened using a sequential stream is called a sequential-access file Java provides the RandomAccessFile class to allow data to be read from and written to at any locations in the file M. G. Abbas Malik, FCIT, UoJ 81

82 Random Access Files Java provides the RandomAccessFile class to allow data to be read from and written to at any locations in the file A file that is opened using the RandomAccessFile class is known as a random-access file The RandomAccessFile class implements the DataInput and DataOutput interfaces M. G. Abbas Malik, FCIT, UoJ 82

83 Random Access Files M. G. Abbas Malik, FCIT, UoJ 83

84 Random Access Files A random-access file consists of a sequence of bytes A special marker called a file pointer is positioned at one of these bytes A read or write operation takes place at the location of the file pointer When a file is opened, the file pointer is set at the beginning of the file M. G. Abbas Malik, FCIT, UoJ 84

85 Random Access Files LISTING 17.8 TestRandomAccessFile.jav 1 import java.io.*; 2 3 public class TestRandomAccessFile { 4 public static void main(string[] args) throws IOException { 5 try ( // Create a random access file 6 RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw"); 7 ) { 8 // Clear the file to destroy the old contents if exists 9 inout.setlength(0); // Write new integers to the file 12 for (int i = 0; i < 200; i++) 13 inout.writeint(i); // Display the current length of the file 16 System.out.println("Current file length is " + inout.length()); // Retrieve the first number 19 inout.seek(0); // Move the file pointer to the beginning 20 System.out.println("The first number is " + inout.readint()); // Retrieve the second number M. G. Abbas Malik, FCIT, UoJ 85

86 Random Access Files 23 inout.seek(1 * 4); // Move the file pointer to the second number 24 System.out.println("The second number is " + inout.readint()); // Retrieve the tenth number 27 inout.seek(9 * 4); // Move the file pointer to the tenth number 28 System.out.println("The tenth number is " + inout.readint()); // Modify the eleventh number 31 inout.writeint(555); // Append a new number 34 inout.seek(inout.length()); // Move the file pointer to the end 35 inout.writeint(999); // Display the new length 38 System.out.println("The new length is " + inout.length()); // Retrieve the new eleventh number 41 inout.seek(10 * 4); // Move the file pointer to the eleventh number 42 System.out.println("The eleventh number is " + inout.readint()); 43 } 44 } 45 } M. G. Abbas Malik, FCIT, UoJ 86

87 Advanced Programming Topics Covered Recursion Revisited Review of Object Oriented Concepts Exception Handling File and Stream Handling Binary I/O Multithreading and Parallel Programming Networking - Socket Programming Java Database Programming M. G. Abbas Malik, FCIT, UoJ 87

Binary I/O. CSE 114, Computer Science 1 Stony Brook University

Binary I/O. CSE 114, Computer Science 1 Stony Brook University Binary I/O CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 Motivation Data stored in a text files is represented in humanreadable form Data stored in a binary files

More information

Optional Lecture Chapter 17 Binary IO

Optional Lecture Chapter 17 Binary IO Optional Lecture Chapter 17 Binary IO COMP217 Java Programming Spring 2017 Text: Liang, Introduction to Java Programming, 10 th Edition Chapter 17 Binary IO 1 Motivations Data stored in a text file is

More information

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

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Chapter 17 Binary I/O 1 Motivations Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are

More information

Lecture 7. File Processing

Lecture 7. File Processing Lecture 7 File Processing 1 Data (i.e., numbers and strings) stored in variables, arrays, and objects are temporary. They are lost when the program terminates. To permanently store the data created in

More information

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

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 9/e Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Data stored in variables and arrays is temporary It s lost when a local variable goes out of scope or when

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

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

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved. Chapter 10 File I/O Copyright 2016 Pearson Inc. All rights reserved. Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program,

More information

I/O Streams. Object-oriented programming

I/O Streams. Object-oriented programming I/O Streams Object-oriented programming Outline Concepts of Data Streams Streams and Files File class Text file Binary file (primitive data, object) Readings: GT, Ch. 12 I/O Streams 2 Data streams Ultimately,

More information

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

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 Chapter 4 Java I/O X i a n g Z h a n g j a v a c o s e @ q q. c o m Content 2 Java I/O Introduction File and Directory Byte-stream and Character-stream Bridge between b-s and c-s Random Access File Standard

More information

Chapter 10. IO Streams

Chapter 10. IO Streams Chapter 10 IO Streams Java I/O The Basics Java I/O is based around the concept of a stream Ordered sequence of information (bytes) coming from a source, or going to a sink Simplest stream reads/writes

More information

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

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO) I/O and Scannar Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 I/O operations Three steps:

More information

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved Comp 249 Programming Methodology Chapter 13 Generics & The ArrayList Class Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has

More information

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

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003 Input, Output and Exceptions COMS W1007 Introduction to Computer Science Christopher Conway 24 June 2003 Input vs. Output We define input and output from the perspective of the programmer. Input is data

More information

Object-Oriented Programming Design. Topic : Streams and Files

Object-Oriented Programming Design. Topic : Streams and Files Electrical and Computer Engineering Object-Oriented Topic : Streams and Files Maj Joel Young Joel Young@afit.edu. 18-Sep-03 Maj Joel Young Java Input/Output Java implements input/output in terms of streams

More information

PIC 20A Streams and I/O

PIC 20A Streams and I/O PIC 20A Streams and I/O Ernest Ryu UCLA Mathematics Last edited: December 7, 2017 Why streams? Often, you want to do I/O without paying attention to where you are reading from or writing to. You can read

More information

Objec&ves. Review. Standard Error Streams

Objec&ves. Review. Standard Error Streams Objec&ves Standard Error Streams Ø Byte Streams Ø Text Streams Oct 5, 2016 Sprenkle - CSCI209 1 Review What are benefits of excep&ons What principle of Java do files break if we re not careful? What class

More information

Performing input and output operations using a Byte Stream

Performing input and output operations using a Byte Stream Performing input and output operations using a Byte Stream public interface DataInput The DataInput interface provides for reading bytes from a binary stream and reconstructing from them data in any of

More information

Data Structures. 03 Streams & File I/O

Data Structures. 03 Streams & File I/O David Drohan Data Structures 03 Streams & File I/O JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN 0132162709 2012 Pearson Education, Inc., Upper Saddle River, NJ.

More information

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

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output COMP 213 Advanced Object-oriented Programming Lecture 19 Input/Output Input and Output A program that read no input and produced no output would be a very uninteresting and useless thing. Forms of input/output

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Files Two types: Text file and Binary file Text file (ASCII file) The file data contains only ASCII values

More information

Object-Oriented Programming in the Java language

Object-Oriented Programming in the Java language Object-Oriented Programming in the Java language Part 5. Exceptions. I/O in Java Yevhen Berkunskyi, NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Exceptions Exceptions in Java are objects. All

More information

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

Software 1 with Java. Recitation No. 9 (Java IO) December 10, Software 1 with Java Recitation No. 9 (Java IO) December 10, 2006 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files

More information

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 11 I/O File Input and Output Reentering data all the time could get tedious for the user. The data can be saved to

More information

Many Stream Classes. File I/O - Chapter 10. Writing Text Files. Text Files vs Binary Files. Reading Text Files. EOF with hasmoreelements()

Many Stream Classes. File I/O - Chapter 10. Writing Text Files. Text Files vs Binary Files. Reading Text Files. EOF with hasmoreelements() File I/O - Chapter 10 Many Stream Classes A Java is a sequence of bytes. An InputStream can read from a file the console (System.in) a network socket an array of bytes in memory a StringBuffer a pipe,

More information

Chapter 11: Exceptions and Advanced File I/O

Chapter 11: Exceptions and Advanced File I/O Chapter 11: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 11 discusses the following main topics:

More information

Exceptions and Working with Files

Exceptions and Working with Files Exceptions and Working with Files Creating your own Exceptions. You have a Party class that creates parties. It contains two fields, the name of the host and the number of guests. But you don t want to

More information

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

Software 1 with Java. Recitation No. 7 (Java IO) May 29, Software 1 with Java Recitation No. 7 (Java IO) May 29, 2007 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes

More information

Example: Copying the contents of a file

Example: Copying the contents of a file Administrivia Assignment #4 is due imminently Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in the front office for a demo time Dining Philosophers code is online www.cs.ubc.ca/~norm/211/2009w2/index.html

More information

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

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices Java I/O File Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices Magnetic disks Optical disks Magnetic tapes Sequential

More information

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

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes.  Debugging tools Today Book-keeping File I/O Subscribe to sipb-iap-java-students Inner classes http://sipb.mit.edu/iap/java/ Debugging tools Problem set 1 questions? Problem set 2 released tomorrow 1 2 So far... Reading

More information

Chapter 12: Exceptions and Advanced File I/O

Chapter 12: Exceptions and Advanced File I/O Chapter 12: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All

More information

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

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output Overview 7 Streams and files import java.io.*; Binary data vs textual data Simple file processing - examples The stream model Bytes and characters Buffering Byte streams Character streams Binary streams

More information

Files and Streams

Files and Streams Files and Streams 4-18-2006 1 Opening Discussion Do you have any questions about the quiz? What did we talk about last class? Do you have any questions about the assignment? What are files and why are

More information

FILE I/O IN JAVA. Prof. Chris Jermaine

FILE I/O IN JAVA. Prof. Chris Jermaine FILE I/O IN JAVA Prof. Chris Jermaine cmj4@cs.rice.edu 1 Our Simple Java Programs So Far Aside from screen I/O......when they are done, they are gone They have no lasting effect on the world When the program

More information

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

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת A Typical Program Most applications need to process some input and produce some output based on that input The Java IO package (java.io) is to make that possible

More information

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

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading. Last Time Reviewed method overloading. A few useful Java classes: Other handy System class methods Wrapper classes String class StringTokenizer class Assn 3 posted. Announcements Final on June 14 or 15?

More information

Exceptions Binary files Sequential/Random access Serializing objects

Exceptions Binary files Sequential/Random access Serializing objects Advanced I/O Exceptions Binary files Sequential/Random access Serializing objects Exceptions No matter how good of a programmer you are, you can t control everything. Other users Available memory File

More information

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

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits) The Java I/O System Binary I/O streams (ASCII, 8 bits) InputStream OutputStream The decorator design pattern Character I/O streams (Unicode, 16 bits) Reader Writer Comparing binary I/O to character I/O

More information

Software 1. Java I/O

Software 1. Java I/O Software 1 Java I/O 1 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating files Classes for serializing objects 2 Streams A stream

More information

C17: I/O Streams and File I/O

C17: I/O Streams and File I/O CISC 3120 C17: I/O Streams and File I/O Hui Chen Department of Computer & Information Science CUNY Brooklyn College 4/9/2018 CUNY Brooklyn College 1 Outline Recap and issues Review your progress Assignments:

More information

1.00 Lecture 30. Sending information to a Java program

1.00 Lecture 30. Sending information to a Java program 1.00 Lecture 30 Input/Output Introduction to Streams Reading for next time: Big Java 15.5-15.7 Sending information to a Java program So far: use a GUI limited to specific interaction with user sometimes

More information

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

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output. CSE 143 Java Streams Reading: 19.1, Appendix A.2 GREAT IDEAS IN COMPUTER SCIENCE REPRESENTATION VS. RENDERING 4/28/2002 (c) University of Washington 09-1 4/28/2002 (c) University of Washington 09-2 Topics

More information

Programmierpraktikum

Programmierpraktikum Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 21 05/07/2012 10:31 AM Input / Output Streams 2 of 21 05/07/2012 10:31 AM Java I/O streams

More information

Previous to Chapter 7 Files

Previous to Chapter 7 Files Previous to Chapter 7 Files Recall Scanner from Part I notes. A scanner object can reference a text file Scanner f = new Scanner(new File("file name goes here")); Scanner methods can be applied to reading

More information

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

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide Module 10 I/O Fundamentals Objectives Upon completion of this module, you should be able to: Write a program that uses command-line arguments and system properties Examine the Properties class Construct

More information

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

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015 Announcements Assignment 4 is posted and Due on 29 th of June at 11:30 pm. Course Evaluations due

More information

Streams and File I/O

Streams and File I/O Walter Savitch Frank M. Carrano Streams and File I/O Chapter 10 Objectives Describe the concept of an I/O stream Explain the difference between text and binary files Save data, including objects, in a

More information

Java Programming Lecture 9

Java Programming Lecture 9 Java Programming Lecture 9 Alice E. Fischer February 16, 2012 Alice E. Fischer () Java Programming - L9... 1/14 February 16, 2012 1 / 14 Outline 1 Object Files Using an Object File Alice E. Fischer ()

More information

What is Serialization?

What is Serialization? Serialization 1 Topics What is Serialization? What is preserved when an object is serialized? Transient keyword Process of serialization Process of deserialization Version control Changing the default

More information

C17: File I/O and Exception Handling

C17: File I/O and Exception Handling CISC 3120 C17: File I/O and Exception Handling Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/24/2017 CUNY Brooklyn College 1 Outline Recap and issues Exception Handling

More information

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

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2 Java IO - part 2 BIU OOP Table of contents 1 Basic Java IO What do we know so far? What s next? 2 Example Overview General structure 3 Stream Decorators Serialization What do we know so far? What s next?

More information

Object Oriented Design with UML and Java. PART VIII: Java IO

Object Oriented Design with UML and Java. PART VIII: Java IO Object Oriented Design with UML and Java PART VIII: Java IO Copyright David Leberknight and Ron LeMaster. Version 2011 java.io.* & java.net.* Java provides numerous classes for input/output: java.io.inputstream

More information

IT101. File Input and Output

IT101. File Input and Output IT101 File Input and Output IO Streams A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession. An Input/Output (I/O) Stream represents

More information

Streams and File I/O

Streams and File I/O Chapter 9 Streams and File I/O Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch

More information

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

Core Java Contents. Duration: 25 Hours (1 Month) Duration: 25 Hours (1 Month) Core Java Contents Java Introduction Java Versions Java Features Downloading and Installing Java Setup Java Environment Developing a Java Application at command prompt Java

More information

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

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi CSE 143 Overview Topics Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 2/3/2005 (c) 2001-5, University of Washington 12-1 2/3/2005 (c) 2001-5,

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan Institute of Engineering & Technology for Diploma Studies Streams A stream is a sequence of data. In Java a stream is composed of bytes. In java, 3 streams are created for us automatically. 1. System.out : standard output stream 2. System.in : standard input

More information

CPS122 Lecture: Input-Output

CPS122 Lecture: Input-Output CPS122 Lecture: Input-Output Objectives: Last Revised April 3, 2017 1. To discuss IO to System.in/out/err 2. To introduce the abstract notion of a stream 3. To introduce the java File, Input/OutputStream,

More information

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

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O I/O 10-7-2013 I/O in Java I/O streams vs. Reader/Writer HW#3 due today Reading Assignment: Java tutorial on Basic I/O public class Swimmer implements Cloneable { public Date geteventdate() { return (Date)

More information

Job Migration. Job Migration

Job Migration. Job Migration Job Migration The Job Migration subsystem must provide a mechanism for executable programs and data to be serialized and sent through the network to a remote node. At the remote node, the executable programs

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: www: materials: e-learning environment: office: alt. office: jkizito@cis.mak.ac.ug http://serval.ug/~jona http://serval.ug/~jona/materials/csc1214

More information

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

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166 Lecture 22 Java Input/Output (I/O) Streams Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Topics I/O Streams Writing to a Stream Byte Streams Character Streams Line-Oriented I/O Buffered I/O

More information

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

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20 File IO Computer Science and Engineering College of Engineering The Ohio State University Lecture 20 I/O Package Overview Package java.io Core concept: streams Ordered sequences of data that have a source

More information

CPS122 Lecture: Input-Output

CPS122 Lecture: Input-Output CPS122 Lecture: Input-Output Objectives: Last Revised January 19, 2010 1. To discuss IO to System.in/out/err 2. To introduce the abstract notion of a stream 3. To introduce the java File, Input/OutputStream,

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE 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

More information

CSE 331. Memento Pattern and Serialization

CSE 331. Memento Pattern and Serialization CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/ 1 Pattern: Memento a

More information

CS112 Lecture: Streams

CS112 Lecture: Streams CS112 Lecture: Streams Objectives: Last Revised March 30, 2006 1. To introduce the abstract notion of a stream 2. To introduce the java File, Input/OutputStream, and Reader/Writer abstractions 3. To show

More information

Algorithms. Produced by. Eamonn de Leastar

Algorithms. Produced by. Eamonn de Leastar Algorithms Produced by Eamonn de Leastar (edeleastar@wit.ie) Streams http://www.oracle.com/technetwork/java/javase/tech/index.html Introduction ± An I/O Stream represents an input source or an output destination.

More information

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream 输 入 / 输出 杨亮 流的分类 输 入输出相关类图 OutputStream FileOutputStream DataInputStream ObjectOutputStream FilterInputStream PipedOutputStream DataOutput InputStream DataInputStream PrintStream ObjectInputStream PipedInputStream

More information

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. Representation of Primitive Java Types. CSE143 Sp Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Ch. 16 4/27/2004 (c) 2001-4, University of

More information

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

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE Overview CSE 143 Topics Data representation bits and bytes Streams communicating with the outside world Basic Java files Other stream classes Streams Reading: Sec. 19.1, Appendix A2 11/2/2003 (c) 2001-3,

More information

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero. (טיפול בשגיאות) Exception handling We learn that there are three categories of errors : Syntax error, Runtime error and Logic error. A Syntax error (compiler error) arises because a rule of the language

More information

File IO. Binary Files. Reading and Writing Binary Files. Writing Objects to files. Reading Objects from files. Unit 20 1

File IO. Binary Files. Reading and Writing Binary Files. Writing Objects to files. Reading Objects from files. Unit 20 1 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files Unit 20 1 Binary Files Files that are designed to be read by programs and that consist of a sequence

More information

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

Special error return Constructors do not have a return value What if method uses the full range of the return type? 23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method

More information

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

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1 Objec&ves Standard Error Streams Ø Byte Streams Ø Text Streams Oct 5, 2016 Sprenkle - CSCI209 1 STANDARD ERROR Oct 5, 2016 Sprenkle - CSCI209 2 1 Standard Streams Preconnected streams Ø Standard Out: stdout

More information

Chapter 10 Input Output Streams

Chapter 10 Input Output Streams Chapter 10 Input Output Streams ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai 600 096. Website : www.ictact.in, Email : contact@ictact.in, Phone :

More information

HST 952. Computing for Biomedical Scientists Lecture 8

HST 952. Computing for Biomedical Scientists Lecture 8 Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952 Computing for Biomedical Scientists Lecture 8 Outline Vectors Streams, Input, and Output in Java

More information

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax Tirgul 1 Today s topics: Course s details and guidelines. Java reminders and additions: Packages Inner classes Command Line rguments Primitive and Reference Data Types Guidelines and overview of exercise

More information

Java Input/Output. 11 April 2013 OSU CSE 1

Java Input/Output. 11 April 2013 OSU CSE 1 Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter

More information

Java file manipulations

Java file manipulations Java file manipulations 1 Categories of Java errors We learn that there are three categories of Java errors : Syntax error Runtime error Logic error. A Syntax error (compiler error) arises because a rule

More information

Lecture 11.1 I/O Streams

Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 1 OBJECT ORIENTED PROGRAMMING Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 2 Outline I/O Basics Streams Reading characters and string 21/04/2014 Ebtsam AbdelHakam

More information

File Operations in Java. File handling in java enables to read data from and write data to files

File Operations in Java. File handling in java enables to read data from and write data to files Description Java Basics File Operations in Java File handling in java enables to read data from and write data to files along with other file manipulation tasks. File operations are present in java.io

More information

Techniques of Java Programming: Streams in Java

Techniques of Java Programming: Streams in Java Techniques of Java Programming: Streams in Java Manuel Oriol May 8, 2006 1 Introduction Streams are a way of transferring and filtering information. Streams are directed pipes that transfer information

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 30 April 4, 2018 I/O & Histogram Demo Chapters 28 HW7: Chat Server Announcements No penalty for late submission by tomorrow (which is a HARD deadline!)

More information

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming

More information

School of Informatics, University of Edinburgh

School of Informatics, University of Edinburgh CS1Ah Lecture Note 29 Streams and Exceptions We saw back in Lecture Note 9 how to design and implement our own Java classes. An object such as a Student4 object contains related fields such as surname,

More information

Chapter 12. File Input and Output. CS180-Recitation

Chapter 12. File Input and Output. CS180-Recitation Chapter 12 File Input and Output CS180-Recitation Reminders Exam2 Wed Nov 5th. 6:30 pm. Project6 Wed Nov 5th. 10:00 pm. Multitasking: The concurrent operation by one central processing unit of two or more

More information

pre-emptive non pre-emptive

pre-emptive non pre-emptive start() run() class SumThread extends Thread { int end; int sum; SumThread( int end ) { this.end = end; } public void run() { // sum integers 1, 2,..., end // and set the sum } } SumThread t = new SumThread(

More information

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams 1.00 Lecture 31 Streams 2 Reading for next time: Big Java 18.6-18.8, 20.1-20.4 The 3 Flavors of Streams In Java, you can read and write data to a file: as text using FileReader and FileWriter as binary

More information

Events and Exceptions

Events and Exceptions Events and Exceptions Analysis and Design of Embedded Systems and OO* Object-oriented programming Jan Bendtsen Automation and Control Lecture Outline Exceptions Throwing and catching Exceptions creating

More information

Generalized Code. Fall 2011 (Honors) 2

Generalized Code. Fall 2011 (Honors) 2 CMSC 202H Generics Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using base classes is general, but restricted to a single class hierarchy

More information

CN208 Introduction to Computer Programming

CN208 Introduction to Computer Programming CN208 Introduction to Computer Programming Lecture #11 Streams (Continued) Pimarn Apipattanamontre Email: pimarn@pimarn.com 1 The Object Class The Object class is the direct or indirect superclass of every

More information

File Processing in Java

File Processing in Java What is File I/O? File Processing in Java I/O is an abbreviation for input and output. Input is data coming in at runtime. Input come sin through a mouse, keyboard, touchscreen, microphone and so on. Output

More information

Fundamental language mechanisms

Fundamental language mechanisms Java Fundamentals Fundamental language mechanisms The exception mechanism What are exceptions? Exceptions are exceptional events in the execution of a program Depending on how grave the event is, the program

More information

STREAMS. (fluxos) Objetivos

STREAMS. (fluxos) Objetivos STREAMS (fluxos) Objetivos To be able to read and write files To become familiar with the concepts of text and binary files To be able to read and write objects using serialization To be able to process

More information

Character Stream : It provides a convenient means for handling input and output of characters.

Character Stream : It provides a convenient means for handling input and output of characters. Be Perfect, Do Perfect, Live Perfect 1 1. What is the meaning of public static void main(string args[])? public keyword is an access modifier which represents visibility, it means it is visible to all.

More information

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

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 22: File I/O Jackie Cheung, Winter 2015 Announcements Assignment 5 due Tue Mar 31 at 11:59pm Quiz 6 due Tue Apr 7 at 11:59pm 2 Review 1. What is a graph? How

More information

Active Learning: Streams

Active Learning: Streams Lecture 29 Active Learning: Streams The Logger Application 2 1 Goals Using the framework of the Logger application, we are going to explore three ways to read and write data using Java streams: 1. as text

More information