⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedemo.java

📁 180个针对Java初学者的简单实例,包含了180個適合與初學者的源碼實例
💻 JAVA
字号:
//运用File类的常用方法,获取有关文件属性的信息。
import java.io.File;
class FileDemo 
{
  static void OutOper(String s)
 {
    System.out.println(s);
  }
  public static void main(String args[]) {
    File f1 = new File("/java/COPYRIGHT");
	//调用File类的toString方法输出对象f1的路径
	System.out.println(f1);
	//获取该文件的各个属性和信息
    OutOper("File Name: " + f1.getName());
    OutOper("Path: " + f1.getPath());
    OutOper("Abs Path: " + f1.getAbsolutePath());
    OutOper("Parent: " + f1.getParent());
    OutOper(f1.exists() ? "exists" : "does not exist");
    OutOper(f1.canWrite() ? "is writeable" : "is not writeable");
    OutOper(f1.canRead() ? "is readable" : "is not readable");
    OutOper("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
    OutOper(f1.isFile() ? "is normal file" : "might be a named pipe");
    OutOper(f1.isAbsolute() ? "is absolute" : "is not absolute");
    OutOper("File last modified: " + f1.lastModified());
    OutOper("File size: " + f1.length() + " Bytes");
	//将 f1对象改名为"newFile"
	File f2 = new File("newFile");
	OutOper("\n"+"****now rename the file " + f1 +" to " +f2 +"****");
	f1.renameTo(f2);
	OutOper("name"+f2.getName());
	//检查改名前的那个文件是否存在
	OutOper(f1+"is exist?" +f1.exists());
	//删除改名后的文件
	OutOper("\n"+"****delete"+f2+"****");
	f2.delete();
	OutOper(f2 + "exist?" + f2.exists());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -