In the Java version of date, files are accessed through the File class, but the operation of the File class is limited, and the characteristics of a specific file system cannot be utilized, and the performance is not high. When access error occurs, many people directly return false and do not give exception information. Therefore, Java7 has added two new tool classes Files and Paths to manipulate files. The Files class contains a large number of static methods to manipulate files. Java7 also introduces a Path interface, which represents a platform path that is unrelated to the platform. Regarding its usage, we will introduce it in detail below.
Paths provides two methods to obtain Path objects.
1. get(URL url)
2. get(String first,String… more), this is a method to splice multiple strings into a path
Below is the test code
package com.sdjz.test1;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.nio.charset.Charset;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.attribute.FileTime;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import java.util.List;import java.util.List;import java.util.List;import java.util.List;import java.util. class FilesTest { public static void main(String[] args) throws FileNotFoundException, IOException { Path path = Paths.get("d:", "test.txt"); // Get the absolute path of path System.out.println(path.toAbsolutePath()); // Copy the file Files.copy(Paths.get("d:", "test.txt"), new FileOutputStream("text1.txt")); // Add the read content to the List<String> lists = Files.readAllLines(Paths.get("d:", "est.java")); System.out.println("Readed file:" + lists); System.out.println("Is it writable? --->" + Files.isWritable(Paths.get("d:", "test.txt"))); // Create a collection to add to the file List<String> list = new ArrayList<>(); list.add("adsf"); list.add("test 1"); list.add("test 2"); // Create a collection to add to the file List<String> list = new ArrayList<>(); list.add("adsf"); list.add("test 1"); list.add("test 2"); // Write multiple strings to the file Files.write(Paths.get("d:", "test.txt"), list, Charset.forName("gbk")); // Determine the size of the specified file System.out.println("test.txt"))); // Determine whether it is a hidden file System.out.println("test.txt"))); // Get a Calendar instance to set the time for file modification Calendar ca = Calendar.getInstance(); // Set the time to May 23, 2014 ca.set(2014, 4, 23); // Set the time when the file was last modified Files.setLastModifiedTime(path, FileTime.fromMillis(ca.getTimeInMillis())); }}Run the above program and you will see the following results
The following are the properties of the modified file time after the modification
The above method to access files using Java7's Files tool class and Path interface is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.