Презентация Java 4 WEB. Lesson 10 - IO, NIO онлайн
На нашем сайте вы можете скачать и просмотреть онлайн доклад-презентацию на тему Java 4 WEB. Lesson 10 - IO, NIO абсолютно бесплатно. Урок-презентация на эту тему содержит всего 43 слайда. Все материалы созданы в программе PowerPoint и имеют формат ppt или же pptx. Материалы и темы для презентаций взяты из открытых источников и загружены их авторами, за качество и достоверность информации в них администрация сайта не отвечает, все права принадлежат их создателям. Если вы нашли то, что искали, отблагодарите авторов - поделитесь ссылкой в социальных сетях, а наш сайт добавьте в закладки.
Презентации » Устройства и комплектующие » Java 4 WEB. Lesson 10 - IO, NIO
Оцените!
Оцените презентацию от 1 до 5 баллов!
- Тип файла:ppt / pptx (powerpoint)
- Всего слайдов:43 слайда
- Для класса:1,2,3,4,5,6,7,8,9,10,11
- Размер файла:586.29 kB
- Просмотров:117
- Скачиваний:0
- Автор:неизвестен
Слайды и текст к этой презентации:
№3 слайд
![Glossary File a record within](/documents_6/d486afae834c0f43bb8b9831c1818a64/img2.jpg)
Содержание слайда: Glossary
File – a record within a file system that stores user and system data (java.exe, movie.mp4…)
Directory - a record within a file system that contains files as well as other directories (Program Files, home, …)
Root directory - the topmost directory in the file system (c:\, /, …)
File system - in charge of reading and writing data within a computer (NTFS, FAT32, …)
Path - String representation of a file or directory within a file system (/user/home/zoo.txt, video/fun.3gp, …)
№5 слайд
![File class java.io.File.java](/documents_6/d486afae834c0f43bb8b9831c1818a64/img4.jpg)
Содержание слайда: File class
java.io.File.java - represents the pathname of a particular file or directory on the file system
File class can be used to represent directories as well as files
Does not require file existence for creation of File object
Types
Relative (video/fun.mkv) - concerning current directory
Absolute (/home/video/fun.3gp) - concerning root directory
№7 слайд
![File class methods exists -](/documents_6/d486afae834c0f43bb8b9831c1818a64/img6.jpg)
Содержание слайда: File class methods
exists() - is the file or directory exists.
getName() - the name of the file or directory denoted by this path.
getAbsolutePath() - the absolute pathname string of this path.
isDirectory() - is the file denoted by this path is a directory.
isFile() - is the file denoted by this path is a file.
length() - the number of bytes in the file.
lastModified() - the number of milliseconds since the epoch when the file was last modified.
delete() - delete the file or directory. (if directory, then the directory must be empty).
renameTo(File) - rename the file denoted by this path.
mkdir() - create the directory named by this path.
mkdirs() - create the directory named by this path including any nonexistent parent directories.
getParent() - return the abstract pathname of this abstract pathname’s parent or null
listFiles() - return the files in the directory.
№8 слайд
![Example File file new File](/documents_6/d486afae834c0f43bb8b9831c1818a64/img7.jpg)
Содержание слайда: Example
File file = new File("C:\\video\\fun.3gp");
System.out.println("File Exists: " + file.exists());
if (file.exists()) {
System.out.println("Absolute Path: " + file.getAbsolutePath());
System.out.println("Is Directory: " + file.isDirectory());
System.out.println("Parent Path: " + file.getParent());
if (file.isFile()) {
System.out.println("File size: " + file.length());
System.out.println("File LastModified: " + file.lastModified());
} else {
for (File subfile : file.listFiles()) {
System.out.println("\t" + subfile.getName());
}
}
}
№13 слайд
![Stream types by abstraction](/documents_6/d486afae834c0f43bb8b9831c1818a64/img12.jpg)
Содержание слайда: Stream types by abstraction level
low-level stream
high-level stream (wrapper)
try (FileInputStream fis = new FileInputStream("users.dat");
BufferedInputStream bfis = new BufferedInputStream(fis);
ObjectInputStream objectStream = new ObjectInputStream(bfis)
) {
System.out.println(objectStream.readObject());
}
№15 слайд
![Most common Stream Operations](/documents_6/d486afae834c0f43bb8b9831c1818a64/img14.jpg)
Содержание слайда: Most common Stream Operations
read() - InputStream
read(byte[] b) – InputStream
read(byte[] b, int off, int len) - InputStream
write(…) - OutputStream
close() - InputStream, OutputStream (use automatic resource management, I/O stream)
flush() - OutputStream
boolean markSupported() - InputStream
mark(readlimit)/reset() - InputStream (use markSupported() - before)
skip(n) - InputStream
int available() - InputStream
№17 слайд
![Buffered Input Output Stream](/documents_6/d486afae834c0f43bb8b9831c1818a64/img16.jpg)
Содержание слайда: Buffered Input/Output Stream
void copy(File source, File destination) throws IOException {
try (InputStream in = new BufferedInputStream(new FileInputStream(source));
OutputStream out = new BufferedOutputStream(new FileOutputStream(destination))
) {
byte[] buffer = new byte[1024];
int lengthRead;
while ((lengthRead = in.read(buffer)) > 0) {
out.write(buffer, 0, lengthRead);
out.flush();
}
}
}
№18 слайд
![Object serialization](/documents_6/d486afae834c0f43bb8b9831c1818a64/img17.jpg)
Содержание слайда: Object serialization\deserialization
Implement the java.io.Serializable interface (not all built-in classes are serializable)
Add serialVersionUID
NotSerializableException
public static void main(String[] args) throws Exception {
File usersStore = new File("users.dat");
writeUsers(Arrays.asList(new User("Simon", 12, 3)), usersStore);
System.out.println(getUsers(usersStore));
}
№19 слайд
![Object serialization](/documents_6/d486afae834c0f43bb8b9831c1818a64/img18.jpg)
Содержание слайда: Object serialization\deserialization
class User implements Serializable {
private static final long serialVersionUID = 1864942621136349408L;
String name;
int age;
transient int badHabbits;
public User(String name, int age, int badHabbits) {
this.name = name;
this.age = age;
this.badHabbits = badHabbits;
}
@Override
public String toString() {
return "User{" + "name='" + name + '}';
}
}
№20 слайд
![ObjectOutputStream](/documents_6/d486afae834c0f43bb8b9831c1818a64/img19.jpg)
Содержание слайда: ObjectOutputStream (serialization)
void writeUsers(List<User> users, File dataFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(dataFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(bos)
) {
for (User user : users) {
out.writeObject(user);
}
}
}
№21 слайд
![ObjectInputStream](/documents_6/d486afae834c0f43bb8b9831c1818a64/img20.jpg)
Содержание слайда: ObjectInputStream (deserialization)
List<User> getUsers(File dataFile) throws IOException, ClassNotFoundException {
List<User> users = new ArrayList<User>();
try (FileInputStream fis = new FileInputStream(dataFile);
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream in = new ObjectInputStream(bis)
) {
while (true) {
Object object = in.readObject();
if (object instanceof User){
users.add((User) object);
}
}
} catch (EOFException e) {
// File end reached
}
return users;
}
№23 слайд
![Buffered File Reader Writer](/documents_6/d486afae834c0f43bb8b9831c1818a64/img22.jpg)
Содержание слайда: Buffered File Reader/Writer
void copy(File source, File destination) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(source));
BufferedWriter writer = new BufferedWriter(new FileWriter(destination))
) {
String s;
while ((s = reader.readLine()) != null) {
writer.write(s);
writer.newLine();
}
}
}
№24 слайд
![Console Old school try](/documents_6/d486afae834c0f43bb8b9831c1818a64/img23.jpg)
Содержание слайда: Console
// Old school
try (
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(in)
) {
String userInput = reader.readLine();
System.out.println("You entered the following: " + userInput);
}
// New school
Console console = System.console();
if(console != null) {
String userInput = console.readLine();
console.writer().println ("You entered the following: "+userInput);
}
№25 слайд
![NIO NIO java.nio.file.Path](/documents_6/d486afae834c0f43bb8b9831c1818a64/img24.jpg)
Содержание слайда: NIO/NIO2
java.nio.file.Path – interface represents a hierarchical path on the storage system to a file or directory.
direct replacement for the legacy java.io.File class.
Immutable (thread safe)
Unlike the File class, the Path interface contains full support for symbolic links
java.nio.files.Paths – factory for path instantiation
Unlike the File class, the Path interface contains support for symbolic links
Does not require file existence for creation of Path object
‘.’ - A reference to the current directory
‘..’ - A reference to the parent of the current directory
Path path1 = Paths.get("video/fun.3gp");
Path path2 = Paths.get("c:\\video\\fun.3gp");
Path path3 = Paths.get("/”,“home”,“video”,“fun.3gp");
Path path4 = Paths.get(new URI("http://www.google.com"));
Path path5 = Paths.get(new URI("ftp://username:password@ftp.the-ftp-server.com"));
Path path1 = FileSystems.getDefault().getPath("video/3gp.png");
№28 слайд
![Names hierarchy Path songPath](/documents_6/d486afae834c0f43bb8b9831c1818a64/img27.jpg)
Содержание слайда: Names hierarchy
Path songPath = Paths.get("c:\\home\\video\\..\\audio\\song.flac");
System.out.println(songPath); // c:\home\video\..\audio\song.flac
System.out.println(songPath.getParent()); // c:\home\video\..\audio
Path normalized = songPath.normalize();
System.out.println(normalized); // c:\home\audio\song.flac
System.out.println(normalized.getParent()); // c:\home\audio
Path absolute = normalized.toAbsolutePath();
System.out.println(absolute); // c:\home\audio\song.flac
System.out.println(absolute.getRoot()); // c:\
System.out.println(absolute.getNameCount()); // 3
System.out.println(absolute.getFileName()); // song.flac
System.out.println(absolute.getName(0)); // home
System.out.println(absolute.subpath(1, 3)); // audio\song.flac
№29 слайд
![Relativize, resolve Path path](/documents_6/d486afae834c0f43bb8b9831c1818a64/img28.jpg)
Содержание слайда: Relativize, resolve
Path path1 = Paths.get("C:\\Windows");
Path path2 = Paths.get("C:\\Program Piles\\JetBrains");
Path path3 = Paths.get("JetBrains");
println(path1.relativize(path2)); // ..\Program Piles\JetBrains
println(path2.relativize(path1)); // ..\..\Windows
println(path2.relativize(path3)); // Exception!
Path path4 = Paths.get("IntelliJ IDEA");
println(path2.resolve(path4)); // C:\Program Piles\JetBrains\IntelliJ IDEA
println(path1.resolve(path2)); // C:\Program Piles\JetBrains
№30 слайд
![File existence, print and](/documents_6/d486afae834c0f43bb8b9831c1818a64/img29.jpg)
Содержание слайда: File existence, print and search folder content
try {
println(Paths.get(".").toRealPath());
Path idea = Paths.get("C:\\Program Piles\\JetBrains\\IntelliJ IDEA");
println(idea.toRealPath());
println(idea.toRealPath(LinkOption.NOFOLLOW_LINKS));
Files.list(idea).forEach(c::println);
Files.walk(idea, 1).forEach(c::println); // DFS
Files.find(idea, 1, (p,a) -> p.startsWith("bin")).forEach(c::println);
} catch (IOException e) {
println("Can not read path: " + e.getMessage());
}
№31 слайд
![Interacting with Files Path](/documents_6/d486afae834c0f43bb8b9831c1818a64/img30.jpg)
Содержание слайда: Interacting with Files
Path idea = Paths.get("c:\\Program Files\\JetBrains\\IntelliJ IDEA 2017.1");
Path currentDir = Paths.get("."); // c:\
Files.exists(idea); // true
Files.isDirectory(idea); // true
Files.isSameFile(idea, idea.resolve("bin\\..")); // true
Files.isSameFile(idea, currentDir); // false
Path newIdea = idea.resolveSibling(Paths.get("IntelliJ IDEA 2018.1"));
Files.exists(newIdea); // false
Files.createDirectory(newIdea); // c:\Program Files\JetBrains\IntelliJ IDEA 2018.1
Files.createDirectories(newIdea); // c:\Program Files\JetBrains\IntelliJ IDEA 2018.1
Files.exists(newIdea); // true
№32 слайд
![Interacting with Files. Copy,](/documents_6/d486afae834c0f43bb8b9831c1818a64/img31.jpg)
Содержание слайда: Interacting with Files. Copy, move
Path fun = Paths.get("c:\\Video\\fun.3gp");
Path moreFun = Paths.get("c:\\Video\\moreFun.3gp");
Path moreMoreFun = Paths.get("c:\\Video\\moreMoreFun.3gp");
Files.exists(fun); // true
Files.exists(moreFun); // false
Files.exists(moreMoreFun); // false
Files.copy(fun, moreFun); // shallow copy
Files.exists(moreFun); // true
Files.exists(moreMoreFun); // false
Files.move(fun, moreMoreFun, REPLACE_EXISTING); // shallow move
Files.exists(moreMoreFun); // true
№33 слайд
![Interacting with Files.](/documents_6/d486afae834c0f43bb8b9831c1818a64/img32.jpg)
Содержание слайда: Interacting with Files. Delete
Files.delete(fun); // Exception
Files.deleteIfExists(fun); // silent delete
Files.delete(moreFun); // deleted
Files.delete(moreMoreFun); // deleted
Files.exists(fun); // false
Files.exists(moreFun); // false
Files.exists(moreMoreFun); // false
№35 слайд
![Read Write files with NIO](/documents_6/d486afae834c0f43bb8b9831c1818a64/img34.jpg)
Содержание слайда: Read\Write files with NIO2
Path src = Paths.get("c:\\users.txt");
Path dest = Paths.get("c:\\adminUsers.txt");
try (
BufferedReader reader = Files.newBufferedReader(src, Charset.forName("US-ASCII"));
BufferedWriter writer = Files.newBufferedWriter(dest, Charset.forName("UTF-16"))
) {
String currentLine;
while ((currentLine = reader.readLine()) != null) {
writer.write(currentLine);
}
} catch (IOException e) {
// Handle file I/O exception...
}
№37 слайд
![File Attributes Path file](/documents_6/d486afae834c0f43bb8b9831c1818a64/img36.jpg)
Содержание слайда: File Attributes
Path file = Paths.get("c:\\video\\fun.3gp");
Files.isDirectory(file); // false
Files.isHidden(file); // false
Files.isReadable(file); // true
Files.isExecutable(file); // true
Files.isSymbolicLink(file); // false
Files.getLastModifiedTime(file); // 2017-12-03T13:46:24.818731Z
Files.size(file); // 13
Files.getOwner(file); // BUILTIN\Admin (Alias)
№40 слайд
![Homework Task Download ZIP by](/documents_6/d486afae834c0f43bb8b9831c1818a64/img39.jpg)
Содержание слайда: Homework Task 1
Download ZIP by java program users.zip (direct link for download can be retrieved in browser)
Unzip it by program and find file named 'users.dat' (it can be in a child folder)
Try to deserialize 'users.dat' file into list of user objects (modify User class if needed)
Fix serialization problems in User class
Deserialize 'users.dat' file into list of user objects
int allUsersCount = Count all deserialized users
String mostFavoriteEncodingAmongAllUsers = Find the most favorite encoding among users
Between users who has higher access level than average, find 'int targetUserId' = user id with biggest name length
Write ALL deserialized users names into file1. Every name in separate line (use next encoding '{mostFavoriteEncodingAmongAllUsers}-{targetUserId}' ). Use system temp folder (files should be removed after usage)
Write ALL deserialized users names into file2. Every name in separate line (use next encoding '{mostFavoriteEncodingAmongAllUsers}-{allUsersCount}' ). Use system temp folder (files should be removed after usage)
Answer – found files encodings and the difference of two files sizes in bytes
Cover by unit tests
№41 слайд
![Homework Task public class](/documents_6/d486afae834c0f43bb8b9831c1818a64/img40.jpg)
Содержание слайда: Homework Task 1
public class User {
private final int id;
private final String name;
private final int accessLevel;
private final String favoriteEncoding;
public User(int id, String name, int accessLevel, String favoriteEncoding) {
this.id = id;
this.name = name;
this.accessLevel = accessLevel;
this.favoriteEncoding = favoriteEncoding;
}
/* getters/setter and other are omitted */
}
№42 слайд
![Homework Task Create a](/documents_6/d486afae834c0f43bb8b9831c1818a64/img41.jpg)
Содержание слайда: Homework Task 2
Create a command line application that will create / delete / rename the folder with the specified name. The folder name and the number (or code) of the operation must be passed as incoming parameters in runtime. If folder with such name already exists – prompt user for confirmation of replacement.
№43 слайд
![Homework Task Implement](/documents_6/d486afae834c0f43bb8b9831c1818a64/img42.jpg)
Содержание слайда: Homework Task 3
Implement Copying files. Create an application that will copy the file to the specified folder. In this case, the user should be able to select the type of stream: with and without buffering. Also, the application must display the time for which the operation was performed.
Testing the application by taking a large file (2-4GB for example) and copy it using buffering and without. Compare the execution time.
Скачать все slide презентации Java 4 WEB. Lesson 10 - IO, NIO одним архивом:
Похожие презентации
-
Java 4 WEB. Lesson 3 - OOP
-
Java 4 WEB
-
Java for web. Js & ajax
-
Java for web. Json
-
Java for web. Junit
-
Основы языка Java. Введение в Web-технологии Java SE, Java EE, Java ME
-
Web-технологии: HTML, CSS, JavaScript, JQuery
-
Разработка Internet- и Web-приложений. Изучаем JavaScript. (Лекция 4)
-
Lesson 08. Особенности тестирования web-приложений
-
ИТ инициатива Java 8