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"); } }
相关文章推荐
- RandomAccessFile的基本操作
- IO-File 文件 目录 基本操作 递归 遍历
- Java学习提要——File类的基本操作
- OpenFileDialog的基本操作
- File对象的基本操作学习
- java File基本操作
- Java中File的基本操作
- File对象的基本操作学习
- java File的基本操作实例
- File的基本实用操作
- Directory类(DirectoryInfo类)和Path类与File类的基本方法与操作以及实例
- File对象的基本操作学习
- java File类的基本操作
- File 的基本操作
- RandomAccessFile基本操作
- File类的基本操作之创建文件
- File类的基本操作之RandomAccessFile写入操作
- java File的基本操作实例
- 我的.Net学习之路二——File文件基本操作
- java File的基本操作实例(目录及文件的 新建/拷贝/移动)