java例程练习(File类)

import java.io.File;
import java.io.IOException;

public class TestFile {

	public static void main(String[] args) {
		String separator = File.separator;
		String fileName = "myfile.txt";
		String directory = "mydir1" + separator + "mydir2";
		
		File f = new File(directory, fileName);
		
		if (f.exists()) {
			System.out.println("文件名:" + f.getAbsolutePath());
			System.out.println("文件大小:" + f.length());
		} else {
			f.getParentFile().mkdirs();
			
			try {
				f.createNewFile();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}

}

你可能感兴趣的:(java例程练习(File类))