File类常见方法
来源:互联网 发布:智能问答可编辑软件 编辑:程序博客网 时间:2024/05/01 23:58
import java.io.*;/*File类常见方法:1,创建。 boolean createNewFile();在指定位置创建文件,如果该文件已经存在,则不创建,返回false。 和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。 boolean mkdir():创建文件夹 boolean mkdirs():创建多級文件夹2,删除。 boolean delete();删除失败返回false。 void deleteOnExit():在程序退出时删除指定文件3,判断。 boolean exists() :文件是否存在 isDirectory() isFile() isHidden(); isAbsolute() 是否绝对路径4.获取信息 getName(): getPath(): getParent(): getAbsolutePath() lastModified() 最后修改时间 length() 文件大小*/class FileDemo{ public static void main(String[] args) throws IOException { //consMethod(); method_5(); } public static void method_5() { File f1 = new File(); File f2 = new File(); sop("raname:"+f1.renameTo(f2)); } public static void method_4() { File f = new File("c:\\a.txt"); sop("path:"+f.getPath()); sop("abspath:"+f.getAbsolutePath()); sop("parent:"+f.getParent());//该方法返回的是绝对路径中的父目录。如果获取的是相对路径,返回null. //如果相对路径中有上一层目录那么该目录就是返回结果。 } public static void method_3() throws IOException { File f = new File("file.txt"); //記住在判断文件对象是否是文件或者目录时,必须要先判断该文件对象封装的内容是否存在 //通过exists判断 sop("dir:"+f.isDirectory()); sop("file:"+f.isFile()); sop("Absolute:"+f.isAbsolute());// 是否绝对路径 } public static void method_2() { File f = new File("file.txt"); //sop("execute:"+f.canExecute()); File dir = new File("abc"); sop("mkdir:"+dir.mkdir()); } public static void method_1() throws IOException { File f = new File("file.txt"); f.deleteOnExit();//在程序退出时删除 //sop("create:"+f.createNewFile()); sop("delete:"+f.delete()); } public static void consMethod() { System.out.println("Hello World!"); //创建File对象 //将a.txt封装成file对象。可以将已有的和未出现的文件或者文件夹封装成对象。 File f1 = new File("C:\\abc\\a.txt");//File(String pathname) File f2 = new File("c:\\abc","b.txt");//File(String parent, String child) ,根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。 File d = new File("C:\\abc"); File f3 = new File(d,"c.txt");//File(File parent, String child) sop("f1:"+f1);//打印出来的是路径 sop("f2:"+f2); sop("f3:"+f3); File f4 = new File("c"+File.separator+"abc"+File.separator+"zzz"+File.separator+"a.txt");//File.separator分隔符,可以实现跨平台 } public static void sop(Object obj) { System.out.println(obj); }}
0 0
- File类常见方法
- File类常见方法
- File类常见方法
- File类的常见方法
- 黑马程序员-File类常见方法
- File类、常见方法、递归、Properties类等
- 黑马程序员-File类的常见方法及使用
- file类的常见方法以及案例解释
- Java File常见类小计
- File类的常见知识点
- File操作 - File类/方法
- File类扩展方法
- File 类 常用方法
- File类方法总结
- File类常用方法
- File类常用方法
- File类及其方法
- File类常用方法
- 欢迎使用CSDN-markdown编辑器
- apt-get update GPG 错误:由于没有公钥,无法验证下列签名(两种解决办法)
- 安装ionic报错 { [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/ionic']解决方法
- Javascript对URL进行编码
- Bootstrap datetimepicker 时间插件
- File类常见方法
- Linux基于TCP/IP简单的客户端、服务器通信程序实例
- iOS APP提交上架最新流程
- Python和二进制(1)
- SLA by Short brain-July-2016
- xshell ssh连接远程服务器出现的乱码问题
- CodeForces 289B--中位数
- OneExerciseOne创建一个类,它包含一个int域,一个char域,他们都没有初始化,验证Java执行了默认初始化
- Java 浅析内部类