File 基本操作

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");
  }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值