Path and File Object Methods Flashcards
How to create a Path object?
Path.get(“”);
Path method for returning a File object
Path.toFile();
Path method for returning URI object
Path.toURI();
Path.getPath() is a shorthand for the class?
java.nio.file.FileSystem.getPath()
Medhod of File object to get Path object
File.getPath()
Path method for getting the number of elements in path and a reference to each element.
Path.getNameCount()
Path method for getting the element name. Returns a Path object.
Path.getName()
Path method for checking if the Path object is absolute. Returns boolean.
Path.absolute()
Path method for converting the relative Path object to absolute. Returns Path object
Path.toAbsolute();
Path method for getting Path object representing the filename
Path.getFileName()
Path method for getting Path object representing the parent path.
Path.getParent()
Path method for getting the root path object.
Path.getPath();
Path method which returns relative path referenced by inclusive start index and exclusive end index
Path.subpath(, )
Path method for constructing a relative path from one Path object to another
Path.relativize();
Path path3 = Paths.get(“E:\habitat”);
Path path4 = Paths.get(“E:\sanctuary\raven”);
System.out.println(path3.relativize(path4));
System.out.println(path4.relativize(path3));
..\sanctuary\raven
....\habitat
Both has to be relative. or both has to be absolute.
method for creating a new Path by joining the existing path to the current path
Path.resolve()
There are times that the resulting Path of relativize method contains redundant path. This path method cleans up the path
Path.normalize()
Path object may point to non-existent file. This method talkes a path object and returns a real Path.
object.
Converts relative path to absolute Path.
Calls normalize implicitly.
Path.toRealPath();
Files method for checkin if the Path object exists.
File.exists();
Files method for checking if two Path objects are the same.
File.isSameFile();
Files method for creating directory. Throws exception if the directory already exists.
Files.createDirectory();
Files method for creating directories. Does not throw exception if the File does not exist.
Files.createDirectories();
Files method for copying files and directories . Throws IO Exception.
Files.copy(source Path, destination Path)
Files contain two overloaded methods for coyping files
Files.copy(InputStream, Path);
Files.copy(Path, OutputStream)
Files method for renaming file or directory
Files.move(sourcePath, destinationPath)
Files method for deleting file.
Files.delete(Path);
Files method for deleting files and checks if it exists
Files.deleteIfExists();
Files method for reading the file contents.
Returns BufferedReader object
Files.newBufferedReader(Path, Charset);
Files method for writing file
Return BufferedWriter object
Files.newBufferedWriter(Path, Charset);
Files method for method which reads all of the lines of a text file and returns the results as an ordered List of String values
List lines = Files.readAllLines()
Files method for checking if the Path is regular file.
Files.isRegularFile(Path)
Files method for checking if the Path is a directory.
Files.isDirectory(Path)
Files method for checking if it is a symbolic link
Files.isSymbolicLink(Path)
FIles Method for checking if the file is Hidden
Files.isHidden()
Files method for checking if the file is readable
Files.isReadable()
Files method for checking if the file is executable.
Files.isExecutable(Path)
Files method for checking the size
Files.size()
Files method to get the last modified time.
FileTime ft = Files.getLastModifiedTime()
Files method for setting the last modified time.
Files.setLastModifiedTime(FileTime ft)
Files method for setting and getting the owner
getOwner() and setOwner(Path, UserPrincipal)
UserPrincipal owner = FileSystems.getDefault().getUserPrincipalLookupService()
.lookupPrincipalByName(“jane”);
Path path = …
UserPrincipal owner = path.getFileSystem().getUserPrincipalLookupService()
.lookupPrincipalByName(“jane”);
Files method for getting the read only attributes of the files.
BasicFileAttributes bfa = Files.readAttributes(Path,Class<a>) </a>
Files method for getting the view of attributes and allows changing of attributes.
BasicFileAttributeView bfav = Files.getFileAttributeView()
Files method returns a Stream object that traverses the directory in a depth-first, lazy manner.
Stream sp = Files.walk(path)
Files method for listing the contents of directory
Files.list()
Files method that returns a Stream object to read content and does not suffer from this same issue. The contents of the file are read and processed lazily, which means that only a small portion of the file is stored in memory at any given time.
Stream lines = Files.lines(Path)