Create a new directory in the file - Project experience the number of bytes to be written to the file, and then read the file
1 package Test; 2 import java.io.File; 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 public class FileTest { 8 9 public static void main(String[] args) throws IOException 10 { 11 File file=new File("F:/abc/aa.txt"); 12 File parentFile=file.getParentFile();// Get new directory of the parent directory 13 is IF (! ParentFile.exists ()) 14 { 15 parentFile.mkdirs (); // if not, the parent directory is created, i.e., F: / abc / catalog 16 } . 17 Boolean file.createNewFile = B (); // Create a new file 18 is IF (B) . 19 { 20 is Path Paths.get P = ( "F.: /abc/aa.txt"); // Get Path object of this file 21 is files .write (p, "the content of the test document" .getBytes ( "UTF-. 8")); // write data to a file 22 is Long ; size = Files.size (P) // number of bytes returned file 23 System.out.println ( "total written document" + size + "bytes" ); 24 } 25 the else 26 is { 27 System.out.println ( "file creation failed" ); 28 } 29 } 30 31 is }
The premise of the implementation of the above code is: There is no abc folder F disc, otherwise the file creation failed.