您的位置:首页 > 其它

File 基本操作

2011-09-29 21:38 204 查看
import java.io.File;

public class FileTest {

public static void main(String[] args) {
File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.java");
System.out.println(myFile.isAbsolute());//测试此抽象路径名是否为绝对路径名。
System.out.println(myFile.getParent());//返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
System.out.println(myFile);
/**
* true
* C:\jdk1.5.0
* C:\jdk1.5.0\File.java
*/
System.out.println(myFile.exists());//是否存在,false
System.out.println(myFile.isDirectory());//测试此抽象路径名表示的文件是否是一个目录。
System.out.println(myFile.isFile());//是否是一个标准文件 false
System.out.println(myFile.isHidden());//是否为隐藏文件,false
System.out.println(myFile.canRead());//是否可读,false
System.out.println(myFile.canWrite());//是否可写,false
}

}


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

public class FileTest {

public static void main(String[] args) throws IOException {
File file = new File("C:\\text.txt");

boolean success = file.delete();//删除文件
file.deleteOnExit();//在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。
System.out.println(success);

file.createNewFile();//真正建立文件
file.renameTo(new File("c:\\text.txt"));//重命名
file.setReadOnly();//设置只读

File file2 = new File("c:\\test\\");
file2.mkdir();//建立路径

File tmp = File.createTempFile("Foo", "temp");//建立临时文件,前缀后缀
System.out.println(tmp.getCanonicalPath());//获得临时文件路径

File file3 = new File("c:");
long totalSpace = file3.getTotalSpace();
System.out.println(file3 + "totalSpace:" + totalSpace + "bytes");
long freeSpace = file3.getFreeSpace();
System.out.println(file3 + "freeSapce:" + freeSpace + "bytes");

File[] roots = File.listRoots();//列出可用的文件系统根。

for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Free space = " + roots[i].getFreeSpace());
System.out.println("Usable space = " + roots[i].getUsableSpace());
System.out.println("Total space = " + roots[i].getTotalSpace());
System.out.println();
}

}

}


import java.io.File;
import java.util.Calendar;
import java.util.Date;

public class Main {
public static void main(String[] argv) throws Exception {
File f = new File("name.txt");

if (!f.exists()) {
System.out.println("File not found.");
return;
}
if (f.canRead())
System.out.println("  Readable");
else
System.out.println("  Not Readable");

if (f.canWrite())
System.out.println("  Writable");
else
System.out.println("  Not Writable");
System.out.println("  Last modified on " + new Date(f.lastModified()));

long t = Calendar.getInstance().getTimeInMillis();
if (!f.setLastModified(t))
System.out.println("Can't set time.");

if (!f.setReadOnly())
System.out.println("Can't set to read-only.");

if (f.canRead())
System.out.println("  Readable");
else
System.out.println("  Not Readable");

if (f.canWrite())
System.out.println("  Writable");
else
System.out.println("  Not Writable");
System.out.println("  Last modified on " + new Date(f.lastModified()));

if (!f.setWritable(true, false))
System.out.println("Can't return to read/write.");

if (f.canRead())
System.out.println("  Readable");
else
System.out.println("  Not Readable");

if (f.canWrite())
System.out.println("  Writable");
else
System.out.println("  Not Writable");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: