您的位置:首页 > 其它

[IO]——创建文件

2016-04-04 02:25 337 查看
public class demo03 {
public static void main(String[] args) {
//test1();
//test2();
try {
test3();
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件操作失败");
}
}
public static void test3() throws IOException, InterruptedException{//创建为文件,删除文件
String path="E:/others/www.txt";//con为系统关键字
File src=new File(path);
if (!src.exists()) {
Boolean flag = src.createNewFile();
System.out.println(flag?"成功":"失败");
}
//删除文件
Boolean flag=src.delete();
System.out.println(flag?"成功":"失败");
//static createTempFile(前缀3个字节,后缀默认.temp,默认临时空间)
//static createTempFile(前缀3个字节,后缀默认.temp,目录)
File temp=File.createTempFile("aaa", ".temp",new File("E:/others/"));
Thread.sleep(10000);
temp.deleteOnExit();//程序退出,则自动删除
}
public static void test2(){//判断信息
File src=new File("E:/others/w.png");
System.out.println("文件是否存在  "+src.exists());//文件是否存在
System.out.println("文件是否可写"+src.canWrite());//是否可读写,canwrite(),canread()
//isFile(),isDirectory
if (src.isFile()) {
System.out.println("文件");
}else if(src.isDirectory()){
System.out.println("文件夹");
}else {
System.out.println("文件不存在");
}
System.out.println("是否为绝对路径 "+src.isAbsolute());
System.out.print(src.length());//长度,字节长度,只有文件才能读出长度
}
public static void test1(){//获取文件名的常用方法,建立联系
File src=new File("E:/others/w.png");
System.out.println(src.getName());//返回名称
System.out.println(src.getPath());//如果是绝对路径,返回完整路径,否则返回相对路径
System.out.println(src.getAbsolutePath());//返回绝对路径
System.out.println(src.getParent());//返回上一级目录,如果没有上一级,返回空
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IO