Empty Serializable Example public class Empty Serializable implements
Empty Serializable Example public class Empty. Serializable implements Serializable { @Override public String to. String() { return "Empty. Serializable{}"; } } try (Object. Output. Stream oos = new Object. Output. Stream( new File. Output. Stream("data/empty. Serializable. Example. dat")); Object. Input. Stream ois = new Object. Input. Stream( new File. Input. Stream("data/empty. Serializable. Example. dat"))) { Empty. Serializable empty. Serializable = new Empty. Serializable(); oos. write. Object(empty. Serializable); Object empty. Serializable. Object = ois. read. Object(); System. out. println(empty. Serializable. Object); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 15
Serializable With Fields Example public class Serializable. With. Fields implements Serializable { int i; String s = "Hello World!"; Double d; boolean b = true; } try (Object. Output. Stream oos = new Object. Output. Stream( new File. Output. Stream("data/serializable. With. Fields. Example. dat")); Object. Input. Stream ois = new Object. Input. Stream( new File. Input. Stream("data/serializable. With. Fields. Example. dat"))) { Serializable. With. Fields serializable. With. Fields = new Serializable. With. Fields(); oos. write. Object(serializable. With. Fields); Object serializable. With. Fields. Object = ois. read. Object(); System. out. println(serializable. With. Fields. Object); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 16
Complex Serializable Example class Non. Serializable. Parent { String s 1 = "Non. Serializable. Parent"; public Non. Serializable. Parent() { } } class Serializable. Parent extends Non. Serializable. Parent implements Serializable { String s 2 = "Serializable. Parent"; public Serializable. Parent() { } } public class Complex. Serializable extends Serializable. Parent implements Serializable { String s 3 = "Complex. Serializable"; static String s 4 = "static. String"; transient String s 5 = "transient. String"; } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 17
Complex Serializable Example try (Object. Output. Stream oos = new Object. Output. Stream( new File. Output. Stream("data/complex. Serializable. Example. dat")); Object. Input. Stream ois = new Object. Input. Stream( new File. Input. Stream("data/complex. Serializable. Example. dat"))) { Complex. Serializable complex. Serializable = new Complex. Serializable(); complex. Serializable. s 1 = "new. Non. Serializable. Parent. Value"; complex. Serializable. s 2 = "new. Serializable. Parent. Value"; complex. Serializable. s 3 = "new. Value"; Complex. Serializable. s 4 = "new. Static. String"; complex. Serializable. s 5 = "new. Transient. Value"; oos. write. Object(complex. Serializable); Object complex. Serializable. Object = ois. read. Object(); System. out. println(complex. Serializable. Object); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 18
Serializable With Serial Version Example public class Serializable. With. Serial. Version implements Serializable { private static final long serial. Version. UID = -3536974916609510237 L; String s = "s"; Integer i = 1; Long l = 2 L; boolean b = false; } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 20
Serializable With Serial Version Example try (Object. Output. Stream oos = new Object. Output. Stream( new File. Output. Stream("data/serializable. With. Serial. Version. dat")); Object. Input. Stream ois = new Object. Input. Stream( new File. Input. Stream("data/serializable. With. Serial. Version. dat"))) { Serializable. With. Serial. Version serializable. With. Fields = new Serializable. With. Serial. Version(); oos. write. Object(serializable. With. Fields); Object serializable. With. Fields. Object = ois. read. Object(); System. out. println(serializable. With. Fields. Object); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 21
Externalizable Example public class Externalizable. Example implements Externalizable { String title = "title"; String name = "n@me"; long count = 213 L; int id = 1; public Externalizable. Example() { } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 25
Externalizable Example public void write. External(Object. Output out) throws IOException { out. write. Char('+'); out. write. UTF(title); out. write. Char('+'); byte[] bytes = name. get. Bytes(Standard. Charsets. ISO_8859_1); out. write. Int(bytes. length); for (byte b : bytes) { out. write. Byte(b); } out. write. Char('+'); out. write. Long(count); out. write. Char('+'); out. write. Int(id); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 26
Externalizable Example public void read. External(Object. Input in) throws IOException, Class. Not. Found. Exception { char c = in. read. Char(); if (c == '+') { this. title = in. read. UTF(); } if ((c = in. read. Char()) == '+') { int name. Bytes. Amount = in. read. Int(); byte[] bytes = new byte[name. Bytes. Amount]; for (int i = 0; i < bytes. length; i++) { bytes[i] = in. read. Byte(); } this. name = new String(bytes, Standard. Charsets. ISO_8859_1); } if ((c = in. read. Char()) == '+') { this. count = in. read. Long(); } if ((c = in. read. Char()) == '+') { this. id = in. read. Int(); } } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 27
Externalizable Example try (Object. Output. Stream oos = new Object. Output. Stream( new File. Output. Stream("data/externalizable. Example. dat")); Object. Input. Stream ois = new Object. Input. Stream( new File. Input. Stream("data/externalizable. Example. dat"))) { Externalizable. Example externalizable. Example = new Externalizable. Example(); oos. write. Object(externalizable. Example); Object externalizable. Example. Object = ois. read. Object(); System. out. println(externalizable. Example. Object); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 28
Ссылки на ЭФС Hard link ◦ ограничения ◦ нельзя создать на файл в др. разделе ◦ Files. create. Link(link, existing) Soft link ◦ Files. create. Symbolic. Link(link, target, attrs) ◦ Files. read. Symbolic. Link(link) ◦ Files. is. Symbolic. Link(path) 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 36
Проверка атрибутов ЭФС Files. is. Readable(Path path) Files. is. Writable(Path path) Files. is. Executable(Path path) 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 38
Создание ЭФС Files. create. Directory(Path dir, File. Attribute<? >. . . attrs) throws IOException ◦ можно создать сразу дерево каталогов Files. create. File(Path path, File. Attribute<? >. . . attrs) throws IOException Files. create. Temp. Directory(String prefix, File. Attribute<? >. . . attrs) throws IOException Files. create. Temp. File(String prefix, String suffix, File. Attribute<? >. . . attrs) throws IOException 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 39
Удаление ЭФС Files. delete(Path path) throws IOException Files. delete. If. Exists(Path path) throws IOException 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 40
Копирование ЭФС Files. copy(Path source, Path target, Copy. Option. . . options) throws IOException При копировании каталога, содержимое не копируется Опции копирования ◦ Standard. Copy. Option. REPLACE_EXISTING ◦ Standard. Copy. Option. COPY_ATTRIBUTES ◦ Link. Option. NOFOLLOW_LINKS 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 41
Копирование ЭФС <=> Stream Files. copy(Path source, Output. Stream out) throws IOException Files. copy(Path source, Path target, Copy. Option. . . options) throws IOException 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 42
Список содержимого каталога Метод Files. new. Directory. Stream(dir, filter) Пример: private static void traverse(Path path, int level) throws IOException { System. out. println(prepended. Name(path, level)); if (Files. is. Directory(path)) { try (Directory. Stream<Path> stream = Files. new. Directory. Stream(path); ) { for (Path p : stream) { traverse(p, level + 1); } } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 44
Чтение содержимого файла Статические методы Files List<String> read. All. Lines(Path path, Charset cs) throws IOException byte[] read. All. Bytes(Path path) throws IOException Path write(Path path, byte[] bytes, Open. Option. . . options) throws IOException Path write(Path path, Iterable<? extends Char. Sequence> lines, Charset cs, Open. Option. . . options) throws IOException 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 45
File. Channel Example try (File. Output. Stream fos = new File. Output. Stream("data/nio/file. Channel. Example. dat"); File. Input. Stream fis = new File. Input. Stream("data/nio/file. Channel. Example. dat")) { File. Channel fos. Channel = fos. get. Channel(); Byte. Buffer byte. Buffer = Byte. Buffer. wrap("Some pretty string to be written". get. Bytes()); fos. Channel. write(byte. Buffer); fos. Channel. close(); fos. flush(); fos. close(); File. Channel fis. Channel = fis. get. Channel(); Byte. Buffer dst = Byte. Buffer. allocate(32); fis. Channel. read(dst); System. out. println(new String(dst. array())); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 50
Files. Byte. Channel Example Seekable. Byte. Channel channel = Files. new. Byte. Channel( Paths. get("data/nio/files. Byte. Channel. Example. dat"), Standard. Open. Option. CREATE, Standard. Open. Option. READ, Standard. Open. Option. WRITE); channel. write(Byte. Buffer. wrap("files bytes channel example". get. Bytes())); channel. position(0); Byte. Buffer byte. Buffer = Byte. Buffer. allocate(32); channel. read(byte. Buffer); System. out. println(new String(byte. Buffer. array())); channel. close(); 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 51
Random. Access File Example try (Random. Access. File w. File = new Random. Access. File( "data/additional/random. Access. File. Example", "rw"); Random. Access. File r. File = new Random. Access. File( "data/additional/random. Access. File. Example", "r"); ) { w. File. write. Bytes("Some long string"); System. out. println(r. File. read. Line()); w. File. seek(0); w. File. write. Bytes("re-written"); r. File. seek(0); System. out. println(r. File. read. Line()); } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 55
Рекурсивные операции Files. walk. File. Tree(Paths. get("data"), new Simple. File. Visitor<Path>() { @Override public File. Visit. Result visit. File(Path file, Basic. File. Attributes attrs) throws IOException { print. File. Info(file, attrs); return super. visit. File(file, attrs); } @Override public File. Visit. Result pre. Visit. Directory(Path dir, Basic. File. Attributes attrs) throws IOException { print. File. Info(dir, attrs); return super. pre. Visit. Directory(dir, attrs); } private void print. File. Info(Path path, Basic. File. Attributes attrs) { File. Time creation. Time = attrs. creation. Time(); long size = attrs. is. Directory() ? 0 : attrs. size(); String file. Name = attrs. is. Directory() ? path. get. File. Name(). to. String() + '/' : path. get. File. Name(). to. String(); System. out. println(String. format("%20 s | %15 d | %s", creation. Time. to. String(), size, file. Name)); } }); 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 56
Zip. Example try (Zip. Output. Stream zip. Output. Stream = new Zip. Output. Stream( new File. Output. Stream("data/additional/zip. Example. zip")); Zip. Input. Stream zip. Input. Stream = new Zip. Input. Stream(new File. Input. Stream("data/additional/zip. Example. zip"))) { for (int i = 0; i < 100; i++) { String str = "String #" + i + "n"; zip. Output. Stream. put. Next. Entry(new Zip. Entry(String. value. Of(i))); zip. Output. Stream. write(str. get. Bytes()); zip. Output. Stream. close. Entry(); } zip. Output. Stream. flush(); zip. Output. Stream. close(); Zip. Entry e; byte[] bytes = new byte[64]; while ((e = zip. Input. Stream. get. Next. Entry()) != null) { zip. Input. Stream. read(bytes); System. out. print(new String(bytes)); } } 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 59
Полезные ссылки NIO vs IO NIO tutorial https: //github. com/Change. Request/nio. examples 05. 02. 2016 КУРС ЛЕКЦИЙ ПО ОСНОВАМ ЯЗЫКА JAVA “ЭТО НЕ БАГ, ЭТО ФИЧА!” 63
- Slides: 64