RE:JAVA学习-文件操作-File

  1. 构造方法:File(String pathname) 通过将给定路径名字字符串转换成抽象路径名来创建一个新File实例
    注意: 1.”.”表示:当前目录,在eclipse中当前目录指的是当前类所在项目的根目录(直接指定文件名或目录名,那么该文件或目录 默认是在当前目录中)
    2.File.separator:它表示一个受系统支持的目录层级分割(相当于斜线 Linux用”/” windows用”\”)
    File file = new File(“.”+File.separator+”demo.txt”);
    附:File(File parent,String child)
    根据parent抽象路径名和child路径名字字符串创建一个新File实例
  2. 作用:
    1:访问该文件或目录的属性信息(名字,大小,修改日期等)
    2:操作文件或目录(创建,删除)
    3:若表示的是目录,可以查看该目录中的子项信息
    注意:不能访问一个文件中的数据

  3. 相关操作:
    boolean isFile() 判断当前File对象所表示的是否是一个文件
    long length() 返回File对象所表示的文件所占用的字节量
    boolean exists() (文件或目录)是否存在
    boolean createNewFile() 此路径表示的文件不存在时,则创建该文件
    boolean delete() 注意:删除一个目录的前提条件是该目录必须是一个空目录才可以将其删除
    boolean isDirectory() 判断是否为目录
    boolean mkdir() 创建目录
    boolean mkdirs() mkdirs的会将要创建的目录创建出来的,同时将所有不存在的父目录一同创建出来,而mkdir方法在创建目录时要求父目录必须存在否则创建不成功。

/**
 * 创建一个多级目录
 * @author adminitartor
 *
 */
public class File_mkdirs {
    public static void main(String[] args) {
        /*
         * 在当前目录下创建目录:a/b/c/d/e/f
         */
        File dir = new File(
            "a"+File.separator+
            "b"+File.separator+
            "c"+File.separator+
            "d"+File.separator+
            "e"+File.separator+
            "f"
        );

        if(!dir.exists()){
            /*
             * mkdirs的会将要创建的目录创建出来的
             * 同时将所有不存在的父目录一同创建出来
             * 而mkdir方法在创建目录时要求父目录必须
             * 存在否则创建不成功。
             */
            dir.mkdirs();
            System.out.println("目录创建完毕!");
        }else{
            System.out.println("该目录已存在!");
        }
    }
}
  1. listFiles()方法
    4.1 File[] listFiles() 获取一个目录中的所有子项
    代码演示:
/**
 * 获取一个目录中的所有子项
 *
 */
public class File_listFiles {
    public static void main(String[] args) {
        /*
         * 查看当前目录中的所有子项
         */
        File dir = new File(".");
        /*
         * boolean isFile()
         * 判断当前File表示的是否是一个文件
         * 
         * boolean isDirectory()
         * 判断当前File表示的是否为一个目录
         */
        if(dir.isDirectory()){
            //获取该目录中的所有子项
            File[] subs = dir.listFiles();
            System.out.println("当前目录有"+subs.length+"个子项");//当前目录有7个子项

            for(File sub : subs){
                if(sub.isFile()){
                    System.out.print("文件:");
                }
                if(sub.isDirectory()){
                    System.out.print("目录:");
                }
                System.out.println(sub.getName());
            }
        }

    }
}

4.2 重载的listFiles方法(将该目录中所有满足要求的子项获取到)
代码演示:

/**
 * File提供了一个重载的listFiles方法,该方法
 * 可以指定一个文件过滤器,这样可以将该目录中
 * 所有满足过滤器要求的子项获取到。
 *
 */
public class File_listFiles2 {
    public static void main(String[] args) {
        File dir = new File(".");
        /*
         * 获取当前目录中的所有文件
         */
        File[] subs = dir.listFiles(new FileFilter(){
            public boolean accept(File file) {  
                String name = file.getName();
                System.out.println("正在过滤:"+name);
                return file.isFile();
            }       
        });

        for(File sub : subs){
            System.out.println(sub.getName());
        }
    }
}

附:删除给定的文件或目录(先将该目录清空)—使用递归
代码演示:

public class File_delete3 {
    public static void main(String[] args) {
        File file=new File("a");
        dele(file);
        System.out.println("删除成功");
    }
    public static void dele(File file){
        if(file.exists()){
            if(file.delete()){
                return ;
            }
            else{
                File[] files=file.listFiles();
                for(File f:files){
//                  System.out.println(f.getName());
                    dele(f);
                }
                file.delete();
            }
        }else{
            System.out.println("文件不存在");
        }
    }
}

5.RandomAccessFile 随机访问操作
5.1 构造方法
RandomAccessFile(File file,String mode)
RandomAccessFile(String filename,String mode)
第一个参数是需要访问的文件,而第二个参数则是访问模式—-“rw”:读写模式 “r”:只读模式
5.2 字节数据读写操作
void write(int d) 根据当前指针所在位置处写入一个字节,是将参数int的”低8位”写出
int read() 当前指针位置读取一个byte(8位) 填充到int的低八位, 高24位为0, 返回值范围正数: 0~255, 如果返回-1表示读取到了文件末尾(每次读取后自动移动文件指针, 准备下次读取)
void write(byte[] d) 根据当前指针所在位置处连续写出给定数组中的所有字节
void write(byte[] d,int offset,int len) 根据当前指针所在位置处连续写出给定数组中的部分字节,这个部分是从数组的offset处开始,连续len个字节
int read(byte[] b) 从文件中尝试最多读取给定数组的总长度的字节量,并从给定的字节数组第一个位置开始,将读取到的字节顺序存放至数组中,返回值为实际读取到的字节量
void close()
5.3 文件指针操作
long getFilePointer() 获取当前指针位置
void seek(long pos) 移动指针到指定位置
int skipBytes(int n) 可以尝试跳过输入的 n 个字节以丢弃跳过的字节(返回跳过的实际字节数。如果 n 为负数,则不跳过任何字节)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
D:\home\jdk11\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\showndoc\haikongweishi -Dmaven.home=D:\home\apache-maven-3.6.0 -Dclassworlds.conf=D:\home\apache-maven-3.6.0\bin\m2.conf "-Dmaven.ext.class.path=D:\home\IntelliJ IDEA 2021.1.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\home\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=59520:D:\home\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath D:\home\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2021.1.3 --offline -s D:\home\apache-maven-3.6.0\conf\settings.xml -Dmaven.repo.local=D:\home\apache-maven-3.6.0\mvn_resp install [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.dwi:haikongweishi >------------------------ [INFO] Building haikongweishi 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [WARNING] The POM for org.apache.maven.plugins:maven-shade-plugin:jar:3.2.1 is missing, no dependency information available [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.240 s [INFO] Finished at: 2023-06-08T13:09:36+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Plugin org.apache.maven.plugins:maven-shade-plugin:3.2.1 or one of its dependencies could not be resolved: Cannot access alimaven (http://maven.aliyun.com/nexus/content/groups/public/) in offline mode and the artifact org.apache.maven.plugins:maven-shade-plugin:jar:3.2.1 has not been downloaded from it before. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException Process finished with exit code 1
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值