文件File的基本操作
来源:互联网 发布:java html生成器 编辑:程序博客网 时间:2024/06/10 04:36
package cn.itcast.io.p2.file.demo;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
public class FileMethodDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/*
* File对象的常见方法。
*
* 1,获取。
* 1.1 获取文件名称。
* 1.2 获取文件路径。
* 1.3 获取文件大小。
* 1.4 获取文件修改时间。
*
* 2,创建与删除。
*
* 3,判断。
*
* 4, 重命名
*
223
*/
// getDemo();
// createAndDeleteDemo();
// isDemo();
// renameToDemo();
// listRootsDemo();
}
public static void listRootsDemo() {
File file = new File("d:\\");
System.out.println("getFreeSpace:"+file.getFreeSpace());
System.out.println("getTotalSpace:"+file.getTotalSpace());
System.out.println("getUsableSpace:"+file.getUsableSpace());
// File[] files = File.listRoots();
// for(File file : files){
// System.out.println(file);
// }
}
public static void renameToDemo() {
File f1 = new File("c:\\9.mp3");
File f2 = new File("d:\\aa.mp3");
boolean b = f1.renameTo(f2);
System.out.println("b="+b);
}
public static void isDemo() throws IOException{
File f = new File("aaa");
// f.mkdir();
f.createNewFile();
// boolean b = f.exists();
// System.out.println("b="+b);
// 最好先判断是否存在。
System.out.println(f.isFile());
System.out.println(f.isDirectory());
}
224
public static void createAndDeleteDemo() throws IOException {
File dir = new File("abc\\q\\e\\c\\z\\r\\w\\y\\f\\e\\g\\s");
// boolean b = dir.mkdir();//make directory
// System.out.println("b="+b);
// dir.mkdirs();//创建多级目录
System.out.println(dir.delete());
// System.out.println(dir.delete());
// 文件的创建和删除。
// File file = new File("file.txt");
/*
* 和输出流不一样,如果文件不存在,则创建,如果文件存在,则不创建。
*
*/
// boolean b = file.createNewFile();
// System.out.println("b="+b);
// boolean b = file.delete();
// System.out.println("b="+b);
}
public static void getDemo(){
// File file = new File("E:\\java0331\\day22e\\a.txt");
File file = new File("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("parent:"+file.getParent());
225
System.out.println("name:"+name);
System.out.println("absPath:"+absPath);
System.out.println("path:"+path);
System.out.println("len:"+len);
System.out.println("time:"+time);
System.out.println("str_time:"+str_time);
}
}
0 0
- 文件File的基本操作
- File文件的 基本操作
- File的基本操作
- File类的基本操作之创建文件
- File类的基本操作之删除文件
- File类的基本操作之读出文件路径
- 操作File文件的一些基本打包函数
- File文件的一些基本方法和操作
- File的基本实用操作
- Java File的基本操作
- File类的基本操作
- File类的基本操作
- File文件的基本属性
- IO容器与文件操作(二)File类的基本操作
- 文件的基本操作
- 文件的基本操作
- 文件的基本操作
- 文件的基本操作
- C#的协变性和逆变性
- Apache添加mod_python模块
- 编码 GBK 的不可映射字符
- hibernate4整合spring3.1出现java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider
- jquery 实现iframe 自适应高度
- 文件File的基本操作
- void main(void) - the Wrong Thing
- 设计模式六大原则(5):迪米特法则
- Linux下部署安装教程
- 输入一棵二叉树,求树的深度
- 2014高考英语听力,男约女终于成功了
- 傅里叶分析之掐死教程
- IOS 用封装API AsyncSocket进行网络通信
- 设计模式系列课程24之【备忘录模式】