java中File类的获取功能和修改名字功能




java中File类的获取功能和修改名字功能




  File getAbsoluteFile():获取文件的绝对路径,返回File对象

  String getAbsolutePath():获取文件的绝对路径,返回路径的字符串

  String getParent():获取当前路径的父级路径,以字符串形式返回该父级路径

  File getParentFile():获取当前路径的父级路径,以File对象形式返回该父级路径

  String getName():获取文件或文件夹的名称

  String getPath():获取File对象中封装的路径

  long lastModified():以毫秒值返回最后修改时间

  long length():返回文件的字节数

  boolean renameTo(File dest): 将当前File对象所指向的路径 修改为 指定File所指向的路径



package com.itheima_01;

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

/*
 *	获取功能
 		File getAbsoluteFile()  
 * 		String getAbsolutePath() 
 * 		String getParent()  
 * 		File getParentFile() 
 * 		String getName() 
 * 		String getPath()  
  		long lastModified() 
  		long length() 
  	修改功能:
 		boolean renameTo(File dest)
 */
public class FileDemo4 {
	public static void main(String[] args) throws IOException  {
		//method();
		//method2();
		//method3();
		
		File f = new File("d.txt");
		File f2 = new File("e.txt");
		
		//boolean renameTo(File dest) : 将当前File对象所指向的路径 修改为 指定File所指向的路径
		//注意:修改的文件路径不能存在,如果存在则修改失败
		System.out.println(f.renameTo(f2));
	}

	private static void method3() {
		File f = new File("a.txt");
		File f2 = new File("d:\\a\\b.txt");
		File f3 = new File("b");
		
		//String getName() : 获取文件和文件夹的名称
		//System.out.println(f.getName());
		//System.out.println(f2.getName());
		//System.out.println(f3.getName());
		
		//String getPath() : 返回创建File对象时给的路径
		//System.out.println(f.getPath());
		//System.out.println(f2.getPath());
		//System.out.println(f3.getPath());
		
		//long lastModified() : 以毫秒值的形式返回最后修改时间
		//System.out.println(f.lastModified());
		//2514023311294
		//Date d = new Date(2514023311294L);
		//System.out.println(d.toLocaleString());
		
		//long length() : 返回文件的字节数
		System.out.println(f.length());
		System.out.println(f3.length());
	}

	private static void method2() throws IOException {
		//File f = new File("a.txt");
		
		//File f2 = new File("b","c.txt");
		//System.out.println(f2.createNewFile());
		
		File parent = new File("b");
		File f3 = new File(parent,"c.txt");
		if(!parent.exists()) {
			parent.mkdirs();
		}
		System.out.println(f3.createNewFile());
		
		//String getParent() 
		System.out.println(f3.getParent());
		//File getParentFile() 
		System.out.println(f3.getParentFile());
	}

	private static void method() {
		File f = new File("d:\\a\\b.txt");
		File f2 = new File("a.txt");
		
		//File getAbsoluteFile()  :以File对象的形式返回当前File对象所有指向的绝对路径
		System.out.println(f2.getAbsoluteFile());
		//String getAbsolutePath() : 返回File对象所指向的绝对路径
		System.out.println(f2.getAbsolutePath());
	}

}





  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值