1) Operating documents
Path and Files are newly added classes in Java SE7 that encapsulate all the functions needed to process file systems on user machines. Path represents a sequence of directory names followed by a file name. The first parameter in the path can be the root path. The path starting with the root path is the absolute path, otherwise it is the relative path. If its path is not a legitimate path, an InvalidPathException exception is thrown.
Absolute path:
Path path=Paths.get("G:\\", "temp","temp.txt");
Relative path:
Path path2=Paths.get("temp", "temp.txt");
1.1 Combination or parsing paths are common operations. Calling p.resolve(q) will return a path according to the following rules:
A. If q is an absolute path, the result is Q.
B. Otherwise, according to the rules of the file system, p is followed by q as the result.
Example:
Path p=Paths.get("G:\\", "temp"); Path q=Paths.get("temp.txt"); Path path=p.resolve(q); System.out.println(path); //Result: G:\temp\temp.txt p=Paths.get("G:\\"); q=Paths.get("H:\\","temp"); path=p.resolve(q); System.out.println(path); //Result: H:\temp
2) Reading and writing documents
Files classes are common file operations that become faster, but are better suited for medium-length files. If you want to handle large file lengths or binary files, you should still use well-known streams or Reader s / Writer s.
Files'readAllBytes method reads the file and returns the number of bytes, readAllLines returns a list of String types, and calls the write method to write a list of byte groups or String types.
Example:
//Byte Array Writes to File Content Path q=Paths.get("G:\\","temp.txt"); Files.write(q, "123123".getBytes("UTF-8")); //Read out the byte array and print it out byte[]b=Files.readAllBytes(q); System.out.println(new String (b)); //String Writing to File Content List<String>linesList=new ArrayList<String>(); linesList.add("11111111111111"); linesList.add("22222222222222"); linesList.add("33333333333333"); Files.write(q, linesList); //Print read data List<String>lineStrings=Files.readAllLines(q); for (String itemString : lineStrings) { System.out.println(itemString); }
3) Copy, move and delete files
Files provides easy methods to copy, move and delete files, Files.copy(FromPaht,toPath), Files.move(fromPath,ToPath), Files.deleteIfExists(path), three methods can add a standard CopyOption enumeration parameter (REPLACE_EXISTING, COPY_ATTRIBUTES, ATOMIC_MOVE).
// Copy the file to the specified path Path path=Paths.get("G:\\", "temp.txt"); Path path2=Paths.get("H:\\","temp.txt"); Files.copy(path, path2, StandardCopyOption.REPLACE_EXISTING); // Move the file to the specified path( StandardCopyOption.ATOMIC_MOVE Moving files across disks is not allowed. Moving files across disks can be done by removing the option. Path path3=Paths.get("F:\\", "temp.txt"); Files.move(path2, path3, StandardCopyOption.ATOMIC_MOVE); // Delete files (best use) deleteIfExists Method to delete files, not recommended delete Method to delete a file, because if the file does not report an exception) boolean isDelete=Files.deleteIfExists(path3);
4) Access to file information
//TODO...