黑马程序员--IO---FIle类常用方法

File类(文件和目录路径名的抽象表示形式):

1.用来将文件或者文件夹封装成对象

2.方便对文件和文件夹进行操作

3.File对象可以作为参数传递给流的构造函数

4.了解File类中的常用方法


构造一个File对象

package com.mth.file;

import java.io.File;

/**
 * 
 * @ClassName: FileTest
 * @Description: 构造一个file对象
 * @author mth 75100313@qq.com
 * @date 2014-2-18 下午06:59:11
 * 
 */
public class FileTest {
	/**
	 * 创建File对象
	 * 
	 */
	public static void consMethod() {

		// 将a.txt封装成对象。可以将已有的和未出现的文件和文件夹封装成文件。
		File file = new File("a.txt");

		File file2 = new File("e:\\abc\\a.txt");

		// parent父目录 child 文件名
		File file3 = new File("e:\\abc", "a.txt");

		// File.separator 代表分隔符 跨平台
		File file4 = new File("e:" + File.separator + "abc" + File.separator
				+ "a.txt");
	}

	public static void main(String[] args) {
		consMethod();
	}

}

File类常见方法: 

package com.mth.file;

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

/**
 * 
 * @ClassName: FileTest1
 * @Description: 创建文件
 * @author mth 75100313@qq.com
 * @date 2014-2-18 下午07:40:32
 *  File类常见方法: 
 *  1.创建 
 *		boolean createNewFile() 	在指定位置创建 如果不存在 则建立  返回 true;如果存在 则不创建,返回false。(合理)
 *  							 	 和输出流不一样,输出流对象一建立创建文件。而且文件如果存在,则覆盖。
 *		boolean mkdir()  	  		创建文件夹
 *		boolean mkdirs() 	  		创建多级文件夹
 *  2.删除
 *  	boolean delete()     		删除失败返回false。
 *  	void deleteOnExit()  		在程序,退出时删除指定文件。
 *  3.判断 
 *  	boolean exists()     		判断文件或目录是否存在。
 *		boolean isDirectory()		是否是目录。 
 *	    boolean isFile()  	  		是否是文件。
 *		boolean isHidden()	  		是否隐藏 。
 *		boolean isAbsolute() 		是否是绝对路径
 *  4.获取信息
 *		String getName()  	 		获取文件或目录的名称
 *		String getPath()  			获取相对路径名
 *		String getParent() 			获取父目录名称 ;如果没有父目录,返回null
 *		String getAbsolutePath()	获取绝对路径名
 *		long lastModified()			获取文件最后一次修改的时间
 *		long length()        
 */
public class FileTest1 {
	
	/*
	 * 创建和删除
	 * */
	public static void method_1() throws IOException {
		File file = new File("e:/file.txt");
		
		//创建文件
		file.createNewFile();
		
		//删除文件
		file.delete();
		
		
		//创建文件夹abcd
		File dir=new File("abcd\\abcd");
		//mkdir()只能创建一级目录
		boolean b=dir.mkdir();
		System.out.println("dir:"+b);
		
		//创建多级文件夹ss
		File dirs=new File("ss\\ss\\ss\\ss\\ss");
		//mkdirs()创建多级目录
		boolean bs=dirs.mkdirs();
		System.out.println("dirs:"+bs);
		
	}
	
	/*
	 * 判断
	 * 
	 * */
	public static void method_2() throws IOException{
		File file=new File("abc.txt");
		file.createNewFile();
		System.out.println("file:"+file.exists());
		
	
	}
	
	public static void method_3() throws IOException{
		File file=new File("method_3.txt");
		
		
		//记住在判断文件对象是否是文件或者目录时,必须要求先判断该文件对象封装的内容是否存在、
		//通过exists()来判断
		System.out.println("是否存在?:"+file.exists());
		System.out.println("dir:"+file.isDirectory());
		
	}
	/*
	 * 
	 * 	获取信息
	 * 
	 * */
	public static void method_4() throws IOException{
		
		File file=new File("f://method_4.txt");
		file.createNewFile();
		
		/*
		 * 	f:\abc\method_4.txt
			f:\abc\method_4.txt
			f:\abc
		 * */
		System.out.println(file.getPath());
		System.out.println(file.getAbsolutePath());
		//刚方法返回的是绝对路径中的父目录  如果获取的是相对路径,返回null
		//如果相对路径中有上一层目录 即返回该目录
		System.out.println(file.getParent());
		
		System.out.println(file.lastModified());
		
		System.out.println(file.length());
	}
	
	
	/*
	 * 
	 * boolean renameTo(File dest) 重命名
	 * 
	 * 
	 * */
	public static void method_5() throws IOException{
		//相当于剪切同时把名字更改了
		File file1=new File("f:/method_5.txt");
		File file2=new File("e:/method_rename.txt");
		boolean b=file1.renameTo(file2);
		System.out.println(b);
	}
	
	
	

	public static void main(String[] args) throws IOException {
		method_5();
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值