/
Files and I/O Streams in Java Files and I/O Streams in Java

Files and I/O Streams in Java - PowerPoint Presentation

trish-goza
trish-goza . @trish-goza
Follow
449 views
Uploaded On 2016-04-18

Files and I/O Streams in Java - PPT Presentation

Written by Amir Kirsh Edited by Liron Blecher Agenda System Parameters File Class IO Streams Reading from the standard input Scanner Class Binary files Text files and character encoding 3 ID: 283377

streams files system class files streams class system stream file input scanner character reading binary parameters encoding standard read

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Files and I/O Streams in Java" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Files and I/O Streams in Java

Written by Amir Kirsh, Edited by Liron BlecherSlide2

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide3

3

System Parameters

Even though Java works on different platforms and is designed to be Platform Neutral, there might be cases when you need to get specific environment parameters

Use

System.getProperties

() to get all available properties

Use

System.getProperty

() to get a specific property (you must provide the property name)Slide4

demo

examples.systemproperties

4Slide5

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide6

6

File Class

The File class represents a file or directory

Supports inquiries on the file or directory, such as:

Rename

Open (if it’s a file) to get a stream of bytes for read / write

List all of its child files (if it’s a directory)

Delete it

etc.

Useful method to get all Root files (or directories) of a machine is the static method:

File.listRoots

()

In Java 7 – the entire File API was re-factored:

http://docs.oracle.com/javase/tutorial/essential/io/index.htmlSlide7

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide8

8

I/O Streams

In Java reading and writing data between different sources are all done using the same API –

I/O Streams

I/O Stream

classes represents a stream of bytes / chars

InputStream

InputStreamReader

BufferedReader

DataInputStream

I/O Streams

are not related only to files, we can have a stream of bytes for network sockets, Byte Array or even for a StringSlide9

9

I/O Streams

There are two major families of

I/O Streams

:

Input / Output

streams – used for reading / writing one

byte

at a time from/to a resource

Reader / Writer

– used to convert binary data to

characters

(and again, read/write them one

character

at a time)

Reader and Writer streams wrap regular input/output streams

Buffered streams

(used in both families) are used to read data (either bytes or characters) into an internal

buffer.

This

makes them more efficient and also allows to read characters in lines (and not just one by one)Slide10

10

I/O Streams

– Input / OutputSlide11

11

I/O Streams

– Reader / WriterSlide12

Streams - Closing

After finishing reading / writing from a stream (any

stream), the stream needs to be closed (this is especially true for files, network, hardware, etc.Use the close method to close the stream; since the method declared that it throws an IOException, it is usually called inside a try / catch blockIn Java 7, there’s a shorter way to open a stream and close it using the

try-with-resource statementhttp://java.dzone.com/articles/java-7-new-feature-%E2%80%93 12Slide13

Streams

Tip!When sending an existing object over and over again over an output stream, it might stay in memory on the other side (input stream). In order to be able to send the entire object again call the method

reset before sending the object again.13Slide14

demo

examples.files.TextFile

14Slide15

Agenda

System Parameters

File ClassStreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide16

16

Reading from the standard input

try {

BufferedReader in =

new BufferedReader(

new InputStreamReader(System.in) );

String

str

= "";

while (

str

!= null) {

str

=

in.readLine

();

System.out.println

(

str.toUpperCase

());

}

} catch (

IOException

e) { } Slide17

17

Reading from the standard input

System.in

an object of type

InputStream

A stream of bytes (NOT chars!)

InputStreamReader

A bridge from byte stream to character stream, can read single chars

BufferedReader

adds the method “

readLine

”Slide18

18

Writing to the console

The method

System.out.println

() is a wrapper for this code:

BufferedWriter

consoleWriter

=

new

BufferedWriter

(

new

OutputStreamWriter

(

System.

out

));Slide19

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide20

20

Scanner

Scanner is a helper class for getting input

Scanner s = new Scanner(System.in);

System.out.println

("Please insert a string: ");

String

str

=

s.nextLine

();

System.out.println

("Please insert a number: ");

int

i

=

s.nextInt

(); // may throw

InputMismatchException

Slide21

21

Scanner

The Scanner class is a wrapper for this code:

new

BufferedReader

(new

InputStreamReader(System.

in

));

Slide22

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide23

23

Binary Files

Binary Files hold data in binary format

DataInputStream / DataOutputStream

-- for primitive typesSlide24

demo

examples.files

24Slide25

25

Serialization

Serialization is a mechanism that enables to read/write entire objects over streams (to a file or a server socket).

Using the marker interface Serializable interface and the

ObjectInputStream

/

ObjectOutputStream

to:

readObject

()

writeObject

()

When writing Object, you can set a version for a Class, thus enabling changing the class without the need to change the files it is written on:

http://www.mkyong.com/java-best-practices/understand-the-serialversionuid/Slide26

Agenda

System Parameters

File ClassI/O StreamsReading from the standard inputScanner ClassBinary files

Text files and character encodingSlide27

27

Text Files and Character Encoding

Joel On SW about character encoding:

http://www.joelonsoftware.com/articles/Unicode.html

– a must read!

InputStream

An I/O stream

of bytes (NOT chars!)

InputStreamReader

A bridge from byte stream to character stream, can read single chars

An important parameter is the Charset

Also when constructing a String out of bytes, it’s important to provide the Charset used on the byte array