Содержание
- 2. Lesson goals Data exchange with external resources java.io – I/O: java.nio – NI/O: Non-blocking I/O 2,
- 3. Glossary File – a record within a file system that stores user and system data (java.exe,
- 4. IO
- 5. File class java.io.File.java - represents the pathname of a particular file or directory on the file
- 6. File instantiation File File file = new File("/home/video/fun.3gp"); File parent = new File("/home"); File child =
- 7. File class methods exists() - is the file or directory exists. getName() - the name of
- 8. Example File file = new File("C:\\video\\fun.3gp"); System.out.println("File Exists: " + file.exists()); if (file.exists()) { System.out.println("Absolute Path:
- 9. Streams A long, nearly never-ending “stream of water” with data presented one “wave” at a time.
- 10. Built-in streams System.in System.err System.out
- 11. Stream types by direction Input Output
- 12. Stream types by content Byte Streams (InputStream/OutputStream) Character Streams (Reader/Writer)
- 13. Stream types by abstraction level low-level stream high-level stream (wrapper) try (FileInputStream fis = new FileInputStream("users.dat");
- 14. Examples
- 15. Most common Stream Operations read() - InputStream read(byte[] b) – InputStream read(byte[] b, int off, int
- 16. File Input/Output Stream void copy(File source, File destination) throws IOException { try (InputStream in = new
- 17. Buffered Input/Output Stream void copy(File source, File destination) throws IOException { try (InputStream in = new
- 18. Object serialization\deserialization Implement the java.io.Serializable interface (not all built-in classes are serializable) Add serialVersionUID NotSerializableException public
- 19. Object serialization\deserialization class User implements Serializable { private static final long serialVersionUID = 1864942621136349408L; String name;
- 20. ObjectOutputStream (serialization) void writeUsers(List users, File dataFile) throws IOException { try (FileOutputStream fos = new FileOutputStream(dataFile);
- 21. ObjectInputStream (deserialization) List getUsers(File dataFile) throws IOException, ClassNotFoundException { List users = new ArrayList (); try
- 22. File Reader/Writer void copy(File source, File destination) throws IOException { try (Reader reader = new FileReader(source);
- 23. Buffered File Reader/Writer void copy(File source, File destination) throws IOException { try (BufferedReader reader = new
- 24. Console // Old school try ( InputStreamReader in = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(in)
- 25. NIO/NIO2 java.nio.file.Path – interface represents a hierarchical path on the storage system to a file or
- 26. Path overview
- 27. Backward compatibility File file = new File("video/fun.3gp"); Path path = file.toPath(); Path path = Paths.get("video/fun.3gp"); File
- 28. 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();
- 29. 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));
- 30. File existence, print and search folder content try { println(Paths.get(".").toRealPath()); Path idea = Paths.get("C:\\Program Piles\\JetBrains\\IntelliJ IDEA");
- 31. Interacting with Files Path idea = Paths.get("c:\\Program Files\\JetBrains\\IntelliJ IDEA 2017.1"); Path currentDir = Paths.get("."); // c:\
- 32. Interacting with Files. Copy, move Path fun = Paths.get("c:\\Video\\fun.3gp"); Path moreFun = Paths.get("c:\\Video\\moreFun.3gp"); Path moreMoreFun =
- 33. Interacting with Files. Delete Files.delete(fun); // Exception Files.deleteIfExists(fun); // silent delete Files.delete(moreFun); // deleted Files.delete(moreMoreFun); //
- 34. Encoding ѓ« ў-л© ‚®Їа®б †Ё§-Ё, ‚ᥫҐ--®© Ё ‚ᥣ®-ўбҐЈ® java.nio.charset.Charset java.nio.charset.StandardCharsets The character encoding determines how characters
- 35. Read\Write files with NIO2 Path src = Paths.get("c:\\users.txt"); Path dest = Paths.get("c:\\adminUsers.txt"); try ( BufferedReader reader
- 36. Read\Write files with NIO2 Path src = Paths.get("c:\\users.txt"); try { List users = Files.readAllLines(src); Stream usersStream
- 37. 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);
- 38. IO vs NIO2
- 39. Literature Basic I/O Class Charset Package java.io
- 40. Homework Task 1 Download ZIP by java program users.zip (direct link for download can be retrieved
- 41. Homework Task 1 public class User { private final int id; private final String name; private
- 42. Homework Task 2 Create a command line application that will create / delete / rename the
- 44. Скачать презентацию