import java.io.File;
import java.io.IOException;
public class file {
public static void main(String[] args) {
/*java.io.File类用于表示文件(目录)
*File类只用于表示文件(目录)的信息(名称,大小等),
*不能用于文件内容的访问
*/
//了解构造函数的情况 查帮助
File file=new File("D:\\一些工具\\eclipse\\workspace\\encoding");
System.out.println(file.exists());
if(!file.exists())
file.mkdir();//创建多级目录用file.mkdirs();
else
file.delete();
//是否是一个目录,是目录返回true,不存在或不是目录返回false
System.out.println(file.isDirectory());
//是否是一个文件
System.out.println(file.isFile());
//File file2=new File("D:\\一些工具\\eclipse\\workspace\\encoding\\1.txt");
File file2=new File("D:\\一些工具\\eclipse\\workspace\\encoding","1.txt");
if(!file2.exists())
try {
file2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
file2.delete();
//常用的File对象的API
System.out.println(file);//file.tostring的内容
System.out.println(file.getAbsolutePath());
System.out.println(file.getName());
System.out.println(file2.getName());
System.out.println(file.getParent());
System.out.println(file2.getParent());
System.out.println(file.getParentFile().getAbsolutePath());
} }
    import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date; public class FileMethodDemo { public static void main(String[] args) throws IOException { /*
* File类:
*
* 1.获取文件的绝对路径
* String getAbsolutePath
* 2.获取文件的路径
* String getPath
* 3.获取文件最后一次修改的时间,要求是x年x月x日。时间。
* long lastModified
* 4.文件是否是隐藏的。
* boolean isHidden
* 5.发现File对象封装的文件或者文件夹是可以存在的也可以不存在。
* 那么不存在的可否用file的功能创建呢?
* creatNewFile()
* mkdir()创建文件夹
* mkdirs()创建多层次文件夹
* 删除功能
* delete()
* 6.一个File对象封装的文件或者文件夹到底是否存在呢?
* 判断存在功能
* exists()
* 7. getFreeSpace() 方法是什么意思?用Demo验证,getTotalSpace() getUsableSpace()
* getFreeSpace():空闲空间
* getUsableSpace():可用空间
* getTotalSpace():总容量
*
*
*/ File file = new File("d:\\"); // methodDemo1(file);
// methodDemo2(file); System.out.println(file.getUsableSpace()/1024/1024/1024); } public static void methodDemo2(File file) throws IOException {
//1.创建。
// boolean b = file.createNewFile();//如果文件不存在,则不创建返回false,不存在就创建,成功就返回true
// System.out.println(b);
//2.删除
// boolean a = file.delete();//不走回收站,删除文件夹时,必须保证该文件夹中没有内容,有内容,必须先把内容删除后,再删除文件夹
// System.out.println("delete:"+a); // System.out.println(file.exists()); //3.创建文件夹
boolean c = file.mkdirs();//创建多级目录
System.out.println("dir:"+c); System.out.println("file:"+file.isFile());//File对象不存在时,它既不是文件
System.out.println("dir:"+file.isDirectory());//也不是文件夹 } public static void methodDemo1(File file) {
String path = file.getAbsolutePath();//获取文件对象的绝对路径,即使封装的是相对的,获取的也是绝对的
String path1 = file.getPath();//获取的是file对象中的封装的路径,封装的是什么获取到的就是什么
System.out.println("path="+path);
System.out.println("path1="+path1); long time = file.lastModified();
Date date = new Date(time);
DateFormat dateFormat = DateFormat.getDateTimeInstance();
String t = dateFormat.format(date);
System.out.println(t); boolean b = file.isHidden();
System.out.println("isHiden():"+b);
} }

最新文章

  1. javascript获取浏览器窗口大小
  2. IOS调用WCF服务,WCF服务器进行上传图片
  3. gvim配置
  4. 为什么要加 -moz- -webkit- -ms- -o- ?
  5. α发布后的感想(组长作业)
  6. HDU-----(4858)项目管理(模拟)
  7. android开发关于popupwindow显示关闭的笔记
  8. GitHub托管项目步骤
  9. (转载)sql语句解决分页问题
  10. Girl_iOS100天学iOS的第一天(规划)
  11. Eclipse中pydev开发,python重载__init__报错,是可以正常执行
  12. eclipse web开发Server配置
  13. Hive入门学习--HIve简介
  14. 学习C++后感
  15. 20155326刘美岑 《网络对抗》Exp2 后门原理与实践
  16. encodeURI、encodeURIComponent
  17. Kubernetes1.91(K8s)安装部署过程(五)--安装flannel网络插件
  18. django缓存基于类的视图
  19. Linux 系统位数以及 Linux 软件位数查看
  20. socket.io websocket

热门文章

  1. 原生css实现fullPage的整屏滚动贴合
  2. Java-基础-HashMap
  3. 【Java】数组Array
  4. The Ninth Week (Lucklyzpp)
  5. JS表格显示时间格式
  6. 加班时的灵感迸发,我用selenium做了个窗口化的爬*宝数据。(附源码链接)
  7. 利用Fastjson注入Spring内存马
  8. 2014年3月5日C#训练
  9. [atAGC045B]01 Unbalanced
  10. 提升 RTC 音频体验 - 从搞懂硬件开始