示例:用三种不同的方式创建文件 news1.txt 、news2.txt 、news3.txt
import org.junit.Test;import java.io.File;
import java.io.IOException;public class FileCreate {public static void main(String[] args) {}//方式1 new File(String pathname) 根据路径创建一个File对象@Testpublic void create01(){String filePath = "d:\\news01.txt";File file = new File(filePath);//需要捕获IO异常try {file.createNewFile();System.out.println("方式一:文件创建成功! ");} catch (IOException e) {throw new RuntimeException(e);}}//方式2 new File(File parent,String child) 根据父目录文件+子路径构建@Testpublic void create02(){File parentFile = new File("d:\\");String fileName = "news02.txt";//这里的File对象,在Java中只是一个对象//只有执行了creatNewFile()方法,才会真正的在磁盘中创建该文件File file = new File(parentFile,fileName);//需要捕获IO异常try {file.createNewFile();System.out.println("方式二:文件创建成功~");} catch (IOException e) {throw new RuntimeException(e);}}//方式3 new File(String parent,String child) 根据父目录+子路径构建@Testpublic void creat03(){String parentPath = "d:\\";String fileName = "news03.txt";//这里还可以继续写路径,但由于只有一层所以为文件名File file = new File(parentPath, fileName);try {file.createNewFile();System.out.println("方式三:文件创建成功*");} catch (IOException e) {throw new RuntimeException(e);}}
}
经常使用File类的下列方法获取文件本身的一些信息:
public String getName() //获取文件名字public boolean canRead() //判断文件是否可读public boolean canWrite() //判断文件是否可写public boolean exits() //判断文件是否存在public long length() //获取文件的长度(单位是字节)public String getAbsolutePath() //获取文件的绝对路径public String getParent() //获取文件的父目录public boolean isFile() //判断文件是否是一个普通文件,而不是目录public boolean isDirectory() //判断文件是否是一个目录public boolean isHidden() //判断文件是否是隐藏文件public long lastModified() //获取文件最后修改时间(返回的是1970年午夜至文件最后修改时刻的毫秒数)
@Test注解 可以测试运行方法,不用通过main方法调用
(需要导入包:import org.junit.Test; )
//获取文件的信息@Testpublic void fileInformation(){//先创建对象File file = new File("d:\\news01.txt");//调用相应的方法得到对应信息System.out.println("文件名字: "+file.getName());System.out.println("文件的绝对路径: "+file.getAbsolutePath());System.out.println("文件的父级目录: "+file.getParent());System.out.println("文件大小(字节): "+file.length());//先在文件中写入一个HelloWorldSystem.out.println("文件是否存在: "+file.exists());System.out.println("是否是一个目录: "+file.isDirectory());}
判断d:\news02.txt是否存在,存在就删除
//判断d:\\news02.txt是否存在,存在就删除
@Test
public void m1(){String filePath = "d:\\news02.txt";File file = new File(filePath);if(file.exists()){if(file.delete()){System.out.println(filePath+" 删除成功");}else{System.out.println(filePath+" 删除失败");}}
}
判断目录D:\demo是否存在,如果存在就删除,否则提示不存在
//判断目录D:\\demo是否存在,如果存在就删除,否则提示不存在
//Java编程中,目录也被当做文件(特殊的文件)
@Test
public void m2(){String filePath = "D:\\demo00";File file = new File(filePath);file.mkdir();//创建目录if(file.exists()){if(file.delete()){System.out.println(filePath+"目录删除成功");}else{System.out.println(filePath+"目录删除失败");}}else{System.out.println("该目录不存在");}
}
判断目录D:\demo\a\b\c是否存在,如果不存在就创建
//判断目录D:\\demo\\a\\b\\c是否存在,如果不存在就创建
@Test
public void m3(){String directoryPath = "D:\\demo\\a\\b\\c";File file = new File(directoryPath);if(file.exists()){System.out.println(directoryPath+"存在");}else{if (file.mkdirs()) {//多级目录要用mkdirs 一级用mkdirSystem.out.println(directoryPath+"创建成功");}else {System.out.println(directoryPath + "创建失败");}}
}
public String[] list();//用字符串形式返回目录下的全部文件public File[] listFiles();//用File对象形式返回目录下的全部文件public String[] list(FilenameFilter obj);//字符串形式返回目录下的指定类型的所有文件public File[] listFiles(FilenameFilter obj);//File对象形式返回目录下的指定类型所有文件
创建目录及文件,然后输出
//创建目录及文件,然后输出
@Test
public void list(){String path = "d:\\Test";String fileName1 = "d:\\Test\\test01.doc";String fileName2 = "d:\\Test\\test02.doc";String fileName3 = "d:\\Test\\test03.doc";File filepath = new File(path);File file1 = new File(fileName1);File file2 = new File(fileName2);File file3 = new File(fileName3);if (filepath.mkdir()) {System.out.println("目录创建成功");}try {file1.createNewFile();file2.createNewFile();file3.createNewFile();System.out.println("文件创建成功");}catch(IOException e){throw new RuntimeException(e);}for (String str:filepath.list()) {System.out.println(str);}
}
当要执行一个本地机上可执行的文件时,可以使用 java.lang 包中的 Runtime 类。
打开电脑上的微信:
@Testpublic void run(){File file = new File("D:\\WeChat\\WeChat.exe");//WeChat.exe的存储路径//1. 先申明对象Runtime ec;//2. 使用该类的getRuntime()静态方法创建对象ec = Runtime.getRuntime();//3.调用exec(String command)方法打开一个可执行文件或者执行一个操作try {ec.exec(file.getAbsolutePath());} catch (IOException e) {throw new RuntimeException(e);}}
上一篇:然后是开头,结尾部分
下一篇:王安石名句,明飞曲二首之一