获取:
   1.1获取文本名称
   1.2获取文件路劲
   1.3获取文件大小
   1.4获取文件修改或创建时间

import java.io.File;
import java.text.DateFormat;
import java.util.Date; public class FileGetMethodDemo {
public static void main(String[] args) {
getDemo();
} public static void getDemo() {
File file = new File("F:\\a.txt"); //获取文本名称
String name = file.getName(); //获取文件的绝对路径
String absPath = file.getAbsolutePath(); //获取文件的相对路劲
String path = file.getPath(); //获取文件的大小
long len = file.length(); //获取文件修改时间
long time = file.lastModified(); //格式化时间
Date date = new Date(time);
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String str_time = dateformat.format(date); System.out.println("name="+name+"\nabsPath="+absPath+"\npath="+path+"\nlen="+len+"\ntime="+str_time);
}
}

创建与删除:

  文件的创建与删除

import java.io.File;
import java.io.IOException; public class File_CreatAndDeleteDemo {
public static void main(String[] args) throws IOException {
CreateAndDeleteDemo(); }
public static void CreateAndDeleteDemo() throws IOException {
File file = new File("F:\\a.txt"); //如果指定的文件不存在并成功地创建,则返回 true;如果指定的文件已经存在,则不创建,不会覆盖原有的文件返回 false
boolean b = file.createNewFile();
System.out.println(b); boolean d = file.delete();
System.out.println(d);
}
}

  文件夹的创建与删除

import java.io.File;
import java.io.IOException; public class File_CreatAndDeleteDemo {
public static void main(String[] args) throws IOException {
CreateAndDeleteDemo(); }
public static void CreateAndDeleteDemo() {
File dir = new File("F:\\abc"); //File dir = new File("F:\\abc\\b\\c\\d\\e\\f\\c");
//boolean b = dir.mkdirs();//创建多级目的,删除的话,只能删掉最里面的那个目录 boolean b = dir.mkdir();//只能创建一级目录
System.out.println(b); //boolean d = dir.delete();//如果要删除的文件不为空,则删除不成功
//System.out.println(d);
}
}

判断

import java.io.File;

public class File_isDemo {
public static void main(String[] args) {
isDemo();
} public static void isDemo(){
File file = new File("F:\\a.txt"); file.mkdir(); //最好先判断文件是否存在,可以用于在删除的时候先判断一下文件是否存在,因为有可能文件正在被操作的时候是删除不了的,因为删除调用的是windows底层的方法
boolean b = file.exists();
System.out.println(b);
//判断是否是文件,如果文件不存在为false
System.out.println(file.isFile());
//判断是否是目录
System.out.println(file.isDirectory());
}
}

重命名:

import java.io.File;

public class File_RenameTo {
public static void main(String[] args) {
RenameTo();
}
//重命名还可以用于剪切文件
private static void RenameTo() {
File file1 = new File("F:\\42-IO流(Proes集合的-基本功能).avi");
File file2 = new File("E:\\huangjianfeng.avi");
boolean b = file1.renameTo(file2);
System.out.println(b);
}
}

最新文章

  1. 《转载》使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误
  2. angularJS获取json数据(实战)
  3. 8 步搭建 Node.js + MongoDB 项目的自动化持续集成
  4. EasyUI-panel 内嵌页面上的js无法被执行
  5. paip。java 高级特性 类默认方法,匿名方法+多方法连续调用, 常量类型
  6. iOS开发-友盟分享(1)
  7. UINavigationController基本使用
  8. Sql Server——查询(二)
  9. 20160217.CCPP体系详解(0027天)
  10. Angular系列文章之angular路由
  11. pagex/y offsetx/y screenx/y clientx/y 用法及区别
  12. lvs负载均衡(DR模式)
  13. MySQL skip-character-set-client-handshake导致的一个字符集问题
  14. jQuery Ajax 上传文件夹及文件
  15. 手机访问电脑中部署的tomcat应用
  16. Swift 内存管理
  17. win7共享文件夹设置无密码
  18. SpringBoot 启动报The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8)
  19. 第六周课上测试-3-ch02补充作业
  20. 如何为 smartraiden 贡献代码

热门文章

  1. 【PyTorch深度学习60分钟快速入门 】Part2:Autograd自动化微分
  2. netty源码解解析(4.0)-3 Channel的抽象实现
  3. gulp报错task function must be specified
  4. c# UTF-16转UTF-8 互转
  5. T-SQL:批GO使用实例(十四)
  6. 数据结构-Python3.7<三>
  7. JavaScript中的window对象的属性和方法;JavaScript中如何选取文档元素
  8. POM、STS、IOC、DI、AOP
  9. 2017-11-25 中文代码示例之Spring Boot 1.3.3演示
  10. Linux  改动inittab文件及忘记密码等导致无法进入系统的解决办法