java常用类File

Java File类是java编程中和文件打交道的抽象路径名称,就是内存中一块区域。

具体请查看JAVA API;

代码如下:

package com.study;

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

 
public class ChapFile01 {  
	
	public static void main(String []args) {
		String separator= File.separator ;
		 
		String path= "test1" + separator +"test2"+separator+"test.txt";
		
		
		File file = new File(path); 
		
		System.out.println("路径分割符======"+file.pathSeparatorChar);
		
		if(file.exists()) {
			 System.out.println("文件已经存在");
			 System.out.println("文件的上层目录:"+file.getParent());
			 System.out.println("可以执行:"+file.canExecute());
			 System.out.println("可以读:"+file.canRead());
			 System.out.println("可以写:"+file.canWrite());
			// System.out.println("删除:"+file.delete());
			 System.out.println("返回文件路径的绝对形式:"+file.getAbsolutePath());
			 System.out.println("返回文件路径的绝对形式:"+file.getAbsoluteFile());
			 System.out.println(file.getFreeSpace());
			 System.out.println(file.getName());
			 
			 
			 String [] strFile = file.getParentFile().list();
			 
			 System.out.println(strFile[0]);
			 
			 try {
				System.out.println(file.getCanonicalPath());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		else
		{
			//file.mkdirs();	//把 path中所有的/当作路径创建
			 file.getParentFile().mkdirs();
			
			
			System.out.println(file.getParentFile());
			
			try
			{
				 file.createNewFile();
				 System.out.println("文件创建成功");
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		} 
	}
}

 

你可能感兴趣的:(java,编程)