Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Java I/O Streams: FileOutputStream, BufferedInputStream, DataInputStream, Ob, Assignments of Computer Science

An in-depth analysis of java input and output streams, focusing on fileoutputstream, bufferedinputstream, datainputstream, and objectinputstream. Topics covered include constructing 'stacks' of streams, writing to and reading from files, and the advantages and disadvantages of arrays. Additionally, the document discusses the java collections framework and its use with lists.

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-9y0
koofers-user-9y0 🇺🇸

10 documents

1 / 63

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
• Homework 5 due today.
CS251 Intermediate Programming
Lecture 19
Data Structures #1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f

Partial preview of the text

Download Understanding Java I/O Streams: FileOutputStream, BufferedInputStream, DataInputStream, Ob and more Assignments Computer Science in PDF only on Docsity!

• Homework 5 due today. CS251 Intermediate Programming

Lecture 19

Data Structures #

Estimated Grade Ranges

Each question was

graded on a 5-point

scale. The total possible

was 65.

  1. True or false? Which things are true about streams?

Lecture 14, p. 8

(^) Streams provide random access to the bytes in a

(^) fi le.

fi^ Streams provide a common means for accessing data in les, network hosts, and other devices.

(^) Input streams will supply data endlessly or until closed.

e^ Accessing stream data in small chunks is often more  cient than in large chunks.

(^) Streams deal primarily with signed bytes.

F F F T F

Which type of stream do these statements relate to?3. Input Stream (I), Output Stream (O), or both (B)?

Lecture 14

  • Has a

(^) write(int)

(^) method.

  • Some methods return• Has byte-sized data and block data transfer methods.

(^) -

(^) to indicate end-of-stream.

  • All methods can throw

(^) IOException

.

B^ O

B I

5a. What method(s) must be called on an

(^) InputStream

when you are

(^) fi nished with it? Why?

close()

(^) must be called to return limited resources to the

attempts to instantiateoperating system. Not closing streams can cause further

(^) FileInputStream

s to fail.

OutputStream 5b. What method(s) must be called on an

(^) when you are

(^) fi nished with it? Why?

flush()

(^) must be called followed by

close()

(^) to ensure

that all output data is forced to the device.

Lecture 14, pg. 23

  1. Assume the following code (where

(^) is

(^) is a valid,

initialized

(^) InputStream

). The

(^) InputStream

for (int i = 0; i < buffer.length; i++) byte[] buffer = new byte[256]; contains a long sequence of zero byte values:

buffer[i] = i;

The value of int b = is.read(buffer);

(^) b = 16

, what then is the value of

(^) buffer[16]

?

Lecture 14, pg. 15

0 1 2 3 4 5 6 7 8

9 10 11 12 13 14 15 16 17

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 16 17

Value Index

Changed by

(^) read()

(^) method

Changed by

(^) for

(^) loop

(^) ObjectOutputStream

(^) (O) or manual serialization

apply to?(M)? To which type of serialization do these statements

Lecture 17, p. 4

(^) You can design the binary format.

so^ Automatically adds information about the objects’ classes,

(^) fi les tend to be a little larger.

(^) Can be used to exchange objects with non-Java programs.

(^) Requires you to write less code.

OM O M

  1. Match the stream class with its description:

Lecture 17, pg. 36

BufferedInputStream

    (^) Converts stream data from bytes to instances of classes

DataInputStream

    • Converts stream data from bytes to Java primitive types

FileInputStream

  • Provides an alternate way to access to memory using the stream concept

ByteArrayInputStream

(^) •

  • Performs no conversions, but exists to improve the performance of streams

ObjectInputStream

  • Provides access to data contained in secondary storage

Which range of indices of the byte[] fields = new byte[2000];FileOutputStream("Electron.dat"); OutputStream os = new 11. If you have the following code:

(^) fields

(^) array will the

following line of code write to the

(^) fi le?

os.write(fields, 500, 1000);

Lecture 14, pg. 23

500

through

1499

  1. Is

(^) java.io.IOException

(^) a checked or an

whether through the use of a It’s a checked exception, which requires explicit handling, for programming I/O?unchecked exception? What are the consequences of this

(^) throws

(^) clause on I/O

exceptions), or containment within amethods (to delegate responsibility for handling potential

(^) try

/ catch

/ finally

block (to respond to potential exceptions).

Lecture 14, pg. 18

makes it efData structures are things that store objects in a way that

fi cient to look them up later.

right one depends on the application.There are several different ways to look up objects and the

Sometimes you want objects sorted by one of theirSometimes you want to store objects in a certain order.Sometimes you want to look up objects by a "code" or token.

(^) fi elds.

Sometimes you want objects grouped into categories.

lists, sets (sorted and unsorted), and maps. The collections we will deal with primarily come in three kinds:collections to manage objects in various ways.The Java Collections Framework provide a number of different

Arrays have the following advantages:The simplest form of data structure is an array.

Very low overhead: it is the most ef

fi cient data structure in

out of order.Random access: there is no penalty for accessing elementsterms of memory use.

Arrays have the following disadvantages:

Size is

(^) fi xed at creation time. You must either allocate the

one.go through every element in sequence, looking for the right Searching for a particular object is slow because you have tomiddle of the array.You can't insert elements into or delete elements from theof the unique index values you wish to use to hold the object.Arrays don't help you organize data. You pick and keep trackelements to a new, larger array as needed.largest array you will ever need, or manually copy the

8

12

0

2

-

98 -

int[] a

[0] [1] [2] [3] [4] [5] [6] 8

12

0

2

-

98 -

int[] b

[0] [1] [2] [3] [4] [5] [6]

8

12

0

2

-

98 -

int[] a

[0] [1] [2] [3] [4] [5] [6] 8

12

0

2

-

98 -

int[] b

[0] [1] [2] [3] [4] [5] [6]

7

b[3] = 7;