Path basePath = Paths.get(baseDir) // OK that baseDir has separatorsĪ path does not have to correspond to a file that actually exists. May be a string such as /opt/myprog or c:\Program Files\myprog For example, you can read a path from a configuration file like this: String baseDir = props.getProperty("base.dir") The get method can get a single string containing multiple components. It then parses the result, throwing an InvalidPathException if the result is not a valid path in the given file system. The static Paths.get method receives one or more strings, which it joins with the path separator of the default file system ( / for a UNIX-like file system, \ for Windows). Path relative = Paths.get("myprog", "conf", "user.properties") Path absolute = Paths.get("/home", "harry") For the absolute path, we assume a UNIX-like file system. For example, here we construct an absolute and a relative path. A path that starts with a root component is absolute. The permissible root components depend on the file system. The first component of a path may be a root component such as / or C:\. 2.4.1 PathsĪ Path is a sequence of directory names, optionally followed by a file name. We expect them to be very popular with Java programmers and discuss them in depth. They are much more convenient to use than the File class which dates back all the way to JDK 1.0. The Path interface and Files class were added in Java 7. In other words, the input/output stream classes are concerned with the contents of files, whereas the classes that we discuss here are concerned with the storage of files on a disk. For example, the Files class can be used to remove or rename a file, or to find out when a file was last modified. The Path interface and Files class encapsulate the functionality required to work with the file system on the user’s machine. However, there is more to file management than reading and writing. Java get file path of current class how to#You have learned how to read and write data from a file. Core Java, Volume II-Advanced Features, 11th Edition
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |