IO流/File类
1.File类的理解
File位于java.io包下,本章节中涉及到的相关流也都声明在javaa.io包下。 File类的一个对象,对应与操作系统下的一个文件或一个文件目录(或文件夹)
2.内部api使用说明
2.1构造器
public File(String pathname):以pathname为路径创建的File对象
public File(String parent,String child):以parent为父路径,child为子路径创建File对象
public File(File parent,String child):根据一个父File对象和一个子文件路径创建File对象
2.2方法
获取文件的基本信息
public String getName():获取名称
public String getPath():获取路径
public String getAbsolutePath():获取绝对路径
public File getAbsoluteFile():获取绝对路径表示的文件
public String getParent():获取上层文件目录路径,若无,返回null
public long length():获取文件长度(即:字节数),不能获取目录的长度
public long lastModified():获取最后一次修改时间,毫秒值
列出目录的下一级
public String[] list():返回一个String数组,表示该File目录中所有子文件或目录
public File[] listFiles():返回一个File数组,表示该File目录中所有子文件或目录
File类的重命名功能
public boolean renameTo(File dest):把文件重命名为指定的文件路径
判断功能的方法
public boolean exists():此File表示的文件或目录是否实际存在
public boolean isDirectory():此File表示是否为目录
public boolean isFile():此File表示是否为文件
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
创建、删除功能
public boolean createNewFile():创建文件,若文件存在,则不创建,返回false
public boolean mkdir():创建文件目录,如果此文件目录存在,就不创建了,如果此文件目录 的上层目录不存在,也不创建
public boolean mkdirs():创建文件目录,如果上层文件目录不存在,一并创建
public boolean delete():删除文件或文件夹
删除的注意事项:① java中的删除不走回收站。 ② 要删除一个文件目录,请注意该文件目录内不能 包含文件或文件目录
3.概念
绝对路径:以windows为例,包括盘符在内的文件或文件目录的完整路径
相对路径:相对于某一个文件目录来讲的相对的一个位置。 在idea中,如果使用单元测试方法:相对于当前的module来讲 如果使用main()方法,相对于当前的project来讲
4.代码测试
注意:代码使用的是单元测试方法
package IO流.atguigu01_file; import org.junit.Test; import java.io.File; public class FileTest { /** *构造器 * public File(String pathname):以pathname为路径创建的File对象 * public File(String parent,String child) * public File(File parent,String child) * * 文件路径表示方式: * 方式一:绝对路径:以windows为例,包括盘符在内的文件或文件目录的完整路径 * 方式二:相对路径:相对于某一个文件目录来讲的相对的一个位置。 * 在idea中,如果使用单元测试方法:相对于当前的module来讲 * 如果使用main()方法,相对于当前的project来讲 */ @Test public void test1(){ //public File(String pathname) File file1=new File("d:\\io\\hello.txt"); File file2=new File("ab"); System.out.println(file2.getAbsolutePath()); } @Test public void test2(){ //public File(String parent,String child) //参数1:一定是一个文件目录 //参数2:可以是文件也可以是文件目录 File file1=new File("d:\\io","abc.txt"); File file2=new File("abc","a12"); //public File(File parent,String child) //参数1:一定是一个文件目录 ///参数2:可以是文件也可以是文件目录 File file3=new File(file2,"ab.txt"); } // public static void main(String[] args) { // File file2=new File("abc"); // System.out.println(file2.getAbsolutePath()); // } }
package IO流.atguigu01_file; import org.junit.Test; import java.io.File; import java.io.IOException; public class FileTest1 { /** * 获取文件的基本信息 * public String getName():获取名称 * public String getPath():获取路径 * public String getAbsolutePath():获取绝对路径 * public File getAbsoluteFile():获取绝对路径表示的文件 * public String getParent():获取上层文件目录路径,若无,返回null * public long length():获取文件长度(即:字节数),不能获取目录的长度 * public long lastModified():获取最后一次修改时间,毫秒值 */ @Test public void test1(){ File file1=new File("hello.txt"); System.out.println(file1.getName()); System.out.println(file1.getPath()); System.out.println(file1.getAbsolutePath()); System.out.println(file1.getAbsoluteFile()); System.out.println(file1.getParent()); System.out.println(file1.length()); System.out.println(file1.lastModified()); } @Test public void test2(){ File file1=new File("D:\\Devolep\\javacode\\IO流文件"); System.out.println(file1.getName()); System.out.println(file1.getPath()); System.out.println(file1.getAbsolutePath()); System.out.println(file1.getAbsoluteFile()); System.out.println(file1.getParent()); System.out.println(file1.length()); System.out.println(file1.lastModified()); } /** * 列出目录的下一级 * public String[] list():返回一个String数组,表示该File目录中所有子文件或目录 * public File[] listFiles():返回一个File数组,表示该File目录中所有子文件或目录 */ @Test public void test3(){ //public String[] list() File file1=new File("D:\\Devolep\\javacode"); String[] fileArr = file1.list(); for (String s:fileArr){ System.out.println(s); } System.out.println(); //public File[] listFile() File[] files = file1.listFiles(); for (File f : files){ System.out.println(f.getName()); } } /** * File类的重命名功能 * public boolean renameTo(File dest):把文件重命名为指定的文件路径 * * file1.renameTo(file2):要想此方法执行完,返回true。要求 * file1必须存在,且file2必须不存在。且file2所在的文件目录需要存在 */ @Test public void test4(){ File file1=new File("hello.txt"); File file2=new File("D:\\Devolep\\javacode\\abc.txt"); boolean renameSuccess = file1.renameTo(file2); System.out.println(renameSuccess?"重命名成功":"重命名失败"); } /** * 判断功能的方法 * public boolean exists():此File表示的文件或目录是否实际存在 * public boolean isDirectory():此File表示是否为目录 * public boolean isFile():此File表示是否为文件 * public boolean canRead():判断是否可读 * public boolean canWrite():判断是否可写 * public boolean isHidden():判断是否隐藏 */ @Test public void test5(){ File file1=new File("D:\\Devolep\\javacode\\abc.txt"); System.out.println(file1.exists()); System.out.println(file1.isDirectory()); System.out.println(file1.isFile()); System.out.println(file1.canRead()); System.out.println(file1.canWrite()); System.out.println(file1.isHidden()); } /** * 创建、删除功能 * public boolean createNewFile():创建文件,若文件存在,则不创建,返回false * public boolean mkdir():创建文件目录,如果此文件目录存在,就不创建了,如果此文件目录 * 的上层目录不存在,也不创建 * public boolean mkdirs():创建文件目录,如果上层文件目录不存在,一并创建 * public boolean delete():删除文件或文件夹 * 删除的注意事项:① java中的删除不走回收站。 ② 要删除一个文件目录,请注意该文件目录内不能 * 包含文件或文件目录 */ @Test public void test6() throws IOException { File file1=new File("D:\\Devolep\\javacode\\IO流文件\\hello.txt"); //测试文件的创建删除 if(!file1.exists()){ boolean isSuccessed=file1.createNewFile(); if(isSuccessed){ System.out.println("创建成功"); } }else{ System.out.println("此文件已存在"); System.out.println(file1.delete()?"文件删除成功":"文件删除失败"); } } @Test public void test07(){ //前提:d:\\io流文件目录存在,io或io1不存在 File file1=new File("D:\\Devolep\\javacode\\IO流文件\\io"); file1.mkdir();//true File file2=new File("D:\\Devolep\\javacode\\IO流文件\\io1"); file2.mkdirs();//true } @Test public void test08(){ //前提:d:\\io流文件目录存在,io或io1不存在 File file1=new File("D:\\Devolep\\javacode\\IO流文件\\io1\\io3"); file1.mkdir();//false File file2=new File("D:\\Devolep\\javacode\\IO流文件\\io2\\io4"); file2.mkdirs();//true } @Test public void test9(){ File file1=new File("D:\\Devolep\\javacode\\IO流文件\\io2\\io4"); System.out.println(file1.delete()); } }

浙公网安备 33010602011771号