Filenames and Pathnames

1218 ワード

私のjavaの基础はあまり良くなくて、だから毎日すべて时间を使ってjavaを熟知します;
毎日学んだことを貼り付けます.
1. Constructing a Filename Path

String path = File.separator + "a" + File.separator + "b";
System.out.println("-----------> "+path);

2.Creating a File

 File file = new File("filename.txt");
 // Create file if it does not exist
boolean success = file.createNewFile();

3.Getting the Size of a File

File file = new File("filename.txt");
// Get the number of bytes in the file
long length = file.length();
System.out.println("length------------->"+length);

4.Renaming a File or Directory

// File (or directory) with old name
	    File file = new File("filename.txt");
	    // File (or directory) with new name
	    File file2 = new File("file.txt");
	    // Rename file (or directory)
	    boolean success = file.renameTo(file2);
	    if (!success) {
	        // File was not successfully renamed
	    }