Java IO之File类

注:本文主要记录自 《兄弟连_马剑威_JAVA基础_212_IO之File类》

1、File类的基本概念

  • 表示文件和目录路径名的抽象表示形式(File既表示文件也表示文件目录)
  • File类可以实现文件的创建、删除、重命名、得到路径、创建时间等,是唯一与文件有关的操作类。
File类的定义:
  • public class File
  • extends Object
  • implements Serializable ,Compareable<File>

2、File类的操作方法
public static final String separator          :表示路径分隔符“\”
public static final String pathSeparator   :表示路径分隔,表示“;”
public File(String pathname)                  :构造File类实例,要传入路径
public boolean createNewFile() throws IOException      创建新文件
public boolean delete()                           :删除文件
public String getParent()                        :得到文件的上一级路径
public boolean isDirectory()                    :判断给定的路径是否是文件夹

eg:
/**
 * File类的使用
 */
public class FileDemo { 

	public static void main(String[] args) {
		//File.separator : On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\\'
		File file=new File("E:"+File.separator+"FileDemo"+File.separator+"filetest.txt");
		System.out.println(file.getAbsolutePath());
		if (file.exists()) {
			System.out.println("file exists");
		}else {
			System.out.println("file not exists");
			//创建文件
			try {<span style="white-space:pre">	</span>File dir=file.getParentFile();dir.mkdirs();//创建父目录文件夹
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//删除文件
		//file.delete();
		//得到文件的上一级路径
		System.out.println("file.getParent(): "+file.getParent());
		//判断一个路径是否是文件夹
		System.out.println("是否是文件夹: "+file.isDirectory());
		//判断一个路径是否是文件
		System.out.println("是否是文件: "+file.isFile());
		
	}

}

public boolean isFile()      判断给定的路径是否是文件
public String[] list()       列出文件夹中的文件,文件名String
public File[] listFiles()     列出文件夹中的所有文件
public boolean mkdir()   创建新的文件夹
public Boolean renameTo(File file)    为文件重命名
public long length()     返回文件大小
String getPath()    路径名字符串



辨析:

1、File.mkdir() 与 File.mkdirs()

public boolean mkdir() {} :Creates the directory named by this file, assuming(假设) its parents exist. Use {@link #mkdirs} if you also want to create missing parents.
即:
mkdir的创建 需要他的父文件夹必须存在
mkdirs的创建 如果父文件夹不存在的话 也会顺便创建父文件夹

比如 /storage/emulated/0/DCIM/Camera 这个路径
如果你手机没有DCIM这个文件夹 你要去创建Camera这个文件夹 就必须用第二种方式创建

2、File.getParent() 与 File.getParentFile()
 /**
* Returns the pathname of the parent of this file. This is the path up to
* but not including the last name. {@code null} is returned if there is no
* parent.
* @return this file's parent pathname or {@code null}.
*/
public String getParent() {}

/**
* Returns a new file made from the pathname of the parent of this file.
* This is the path up to but not including the last name. {@code null} is
* returned when there is no parent.
*
* @return a new file representing this file's parent or {@code null}.
*/
public File getParentFile() {}
所以主要在与返回值类型的不同
getParent() 返回的是父目录的路径,为字符串类型
getParentFile() 返回的是通过 父目录路径生成的 file文件,为 file 类型

eg:
在E盘的 FileTest 文件夹下 存在文件 filetest.txt
public class FileDemo {
    public static void main(String[] args) {
        File f=new File("E:/FileTest/filetest.txt");//或者 File file=new File("E:\\FileTest\\filetest.txt");
        String parent =f.getParent();
        System.out.println("getParent: "+parent);
        File f1=f.getParentFile();
        String path = f1.getAbsolutePath();
        System.out.println(path);
    }
}
输出:
getParent: E:\FileTest
E:\FileTest

3、File.exists() 测试
同2,在E盘的 FileTest 文件夹下 存在文件 filetest.txt
public class FileDemo {
    public static void main(String[] args) {
        File f=new File("E:/FileTest/filetest.txt");//或者 File file=new File("E:\\FileTest\\filetest.txt");
        if (f.exists()){
            System.out.println("文件存在");
        }
        File f2= new File("E:/FileTest/filetest2.txt");
        if (f2.exists()){
            System.out.println("文件存在");
        }else{
            System.out.println("文件不存在");
        }
    }
}
输出:
文件存在
文件不存在
思考:
File f2= new File("E:/FileTest/filetest2.txt"); 
在 filetest2.txt 文件未存在的情况下,File f2= new File("E:/FileTest/filetest2.txt"); 并未真正创建文件
当调用createNewFile方法时才会真正在该路径下创建文件
f2.createNewFile();






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值