www.javacup.ir sadegh aliakbary. copyright ©2014 javacup.irjavacup.ir all rights reserved....

12
An Introduction to Java Programming www.javacup.ir Sadegh Aliakbary

Upload: trevor-tyler

Post on 13-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

An Introduction to Java Programming

www.javacup.ir

Sadegh Aliakbary

Page 2: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

2

Copyright ©2014 JAVACUP.IRAll rights reserved. Redistribution of JAVACUP contents is not

prohibited if JAVACUP is clearly noted as the source in the used case.

JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon.

Please send your feedback to [email protected]

JAVACUP.ir

Page 3: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

3

AgendaJava New APIs for IONIONIO.2

JAVACUP.ir

Page 4: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

4

The First Version of Java I/O APIsjava.io packageThe File class limitations:

more significant functionality required (e.g. copy method)

defines many methods that return a Boolean value In case of an error, an exception is better than a

simple false.Poor support for handling symbolic linksinefficient way of handling directories and pathsvery limited set of file attributes

JAVACUP.ir

Page 5: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

5

Java New IO (NIO)Introduced in Java 1.4 (2002)The key features of NIO were:Channels and SelectorsBuffersCharset

java.nio.charsetencoders, and decoders to map bytes and

Unicode symbols

JAVACUP.ir

Page 6: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

6

NIO.2Introduced in Java 1.7 (2011)Java 7 introduces the java.nio.file packageNew interfaces and classes

Path, Paths, and Files

JAVACUP.ir

Page 7: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

7

Path and PathsPath is an interface while Paths is a class

JAVACUP.ir

Page 8: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

8

Path interface

The toPath() method in the java.io.File class returns the Path object; this method was added

in Java 7The toFile() method in the Path interface to

get a File object

JAVACUP.ir

Page 9: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

9

The Files Classthe java.nio.file packageProvides static methods for copy, move,

delete, …New methods for

Symbolic linked filesAttributes…

JAVACUP.ir

Page 10: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

10

copyPath pathSource = Paths.get(str1);Path pathDestination = Paths.get(str2);Files.copy(pathSource, pathDestination);

it will not copy the files/directories contained in the source directory

you need to explicitly copy them to the destination folder

JAVACUP.ir

Page 11: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

11

Listening for ChangesPath path = Paths.get("..\\src");WatchService watchService = null;

watchService = path.getFileSystem().newWatchService();path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);

JAVACUP.ir

Page 12: Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP

Contents redistribution is allowed if JAVACUP is noted as the source

12JAVACUP.ir