You are on page 1of 17

JavaSvet

Novosti u Javi 7
Project COIN, Novi I/O

http://javasvet.rs

info@javasvet.rs

JavaSvet
Project COIN

Sitne izmene koje ivot lepim 5 novina:


<> dijamant itljivija notacija velikih brojeva Switchu stringu Multi-catch Try-with-resources

http://javasvet.rs

info@javasvet.rs

JavaSvet
<>

Pre generika List list = new ArrayList() Sa genericima List<List<String>> list = new ArrayList<List<String>>() Dijamantska verzija List<List<String>> list = new ArrayList<>()
http://javasvet.rs info@javasvet.rs

JavaSvet
<>

Preterani primer Map<Map<String, List<String>, List<List<String>>> mapOfMaps = new HashMap<>();

http://javasvet.rs

info@javasvet.rs

JavaSvet itljivija notacija velikih brojeva


long ssn = 456_78_9999L; byte nybbles = 0b0010_0101; long hexWords = 0xCAFE_BABE; float pi = 3.14_15F;

Binary integral literals and underscores in numeric literals

http://javasvet.rs

info@javasvet.rs

JavaSvet
String u switch-u
final String day = "weekend"; switch (day) { case "workday": System.out.println("buuu!"); break; case "weekend" System.out.println("jeej!"); break; default: break; }
http://javasvet.rs info@javasvet.rs

JavaSvet
Multi-catch

Pre je bilo ovako


} catch (FirstException ex) { logger.error(ex); throw ex; } catch (SecondException ex) { logger.error(ex); throw ex; }
http://javasvet.rs info@javasvet.rs

JavaSvet
Multi-catch

U Javi 7 mogue je uraditi jednostavnije


} catch (FirstException | SecondException ex) { logger.error(ex); throw ex; }

http://javasvet.rs

info@javasvet.rs

JavaSvet
Multi-catch

Novi exception ReflectiveOperationException Superklasa za sve exceptione vezane za refleksiju

http://javasvet.rs

info@javasvet.rs

JavaSvet
Try-with-resources

Automatsko zatvaranje resursa


try (FileOutputStream fos = new FileOutputStream(file); InputStream is = url.openStream()) { byte[] buf = new byte[2048]; int len; while ((len = is.read(buf)) < 0) { fos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); }

Novi interfejs java.lang.AutoCloseable


http://javasvet.rs info@javasvet.rs

JavaSvet
New I/O zato?

Dosta metoda nije bacalo exception (delete, mkdir vraaju bool umesto exceptiona) Trenutno API nije konzistentant na svim platformama Nedostatak osnovnih operacija (copy, move...) Nema notifikacija kada je fajl promenjen
http://javasvet.rs info@javasvet.rs

JavaSvet
Novi I/O API

java.nio.file paket

Path putanja fajla na fajl sistemu Files statike metode za rad za fajlovima/folderima FileSystem pristup fajl sistemu, factory za objekte za pristup fajl sistemu

http://javasvet.rs

info@javasvet.rs

JavaSvet
Novi I/O API

java.nio.file

DirectoryStream iterator, AutoClosable :) Filtriranje, regex FileVisitor

Obilazak putanje walkFileTree(Path, FileVisitor)

WatchService oslukivanje promena na fajl sistemu

http://javasvet.rs

info@javasvet.rs

JavaSvet
Novi I/O API

File Attributes

Pristup bazinim atributima (ime, veliina, timestampovi) Pristup atributima specifini za odreene platforme

Podrka za symbolic links

http://javasvet.rs

info@javasvet.rs

JavaSvet
Novi I/O API

Primeri

Files.copy(InputStream source, Path target, CopyOptions REPLACE_EXISTING); try { Files.delete(path); } catch (NoSuchFileException x) { // No such file } catch (DirectoryNotEmptyException x) { // Folder not empty } catch (IOException x) { // Something else, permissions maybe? }
http://javasvet.rs info@javasvet.rs

JavaSvet
Pitanja?

http://javasvet.rs

info@javasvet.rs

JavaSvet

Hvala na panji
Vuk Nikoli
@vuknikolic vuk.nikolic@javasvet.rs

http://javasvet.rs

info@javasvet.rs

You might also like