博客
关于我
输入输出之File类
阅读量:631 次
发布时间:2019-03-13

本文共 1666 字,大约阅读时间需要 5 分钟。

Java中的File类是用于处理平台无关文件和目录的核心类,支持跨平台操作文件。无论是文件还是目录,都可以通过File对象进行操作和管理。

File类对象表示文件或目录的存在,虽然它本身不会直接访问文件内容,但可以通过操作输入输出流来实现文件内容的读写。这种设计使得File类在不同操作系统间具有良好的抽象性和通用性。

File类的文件路径可以是绝对路径也可以是相对路径,默认情况下使用工作目录作为基准路径。例如:

File file = new File("example.txt"); // 相对路径File dir = new File("myfolder");   // 创建的目录

File类提供了丰富的操作方法,可以实现文件的创建、删除、重命名、权限修改等功能。以下是常用的一些核心操作方法:

  • 创建文件或目录:
  • File file = new File("newfile.txt");try {    file.createNewFile();} catch (IOException e) {    e.printStackTrace();}File dir = new File("myfolder");try {    dir.mkdir();} catch (IOException e) {    e.printStackTrace();}
    1. 文件内容的读写:
    2. try {    FileInputStream fis = new FileInputStream(file);    byte[] bytes = new byte[fis.available()];    fis.read(bytes);    System.out.println(new String(bytes));} catch (IOException e) {    e.printStackTrace();}
      1. 文件权限的管理:
      2. try {    file.setExecutable(true); // 设置可执行权限    file.setReadOnly();     // 设置只读属性} catch (SecurityException e) {    e.printStackTrace();}
        1. 目录的创建与文件的移动:
        2. File srcFile = new File("srcfile.txt");File destFile = new File("newfilepath.txt");try {    Files.move(srcFile, destFile,ugerConfig.get("lara.sft"));} catch (IOException e) {    e.printStackTrace();}

          File类的构造方法主要有以下几种:

          • File(String pathname):根据路径名创建File对象
          • File(String parent, String child):通过父目录和子路径名创建File对象
          • File(String uri):通过URI创建File对象
          • File(File parent, String child):通过父File对象和子路径名创建File对象

          需要注意的是:

          • 只创建File对象并不表示文件或者目录已经存在于文件系统中
          • 调用file.createNewFile()之后,文件才能在文件系统中被真正创建
          • 创建目录时,默认情况下不会递归创建父目录

          File类还提供了丰富的查询方法,例如判断文件是否存在:

          boolean isFileExists = file.exists();

          File类的query方法涵盖了文件和目录的一系列操作,包括引用、重命名、权限管理、最后修改时间等功能。这些方法为Java程序员提供了对文件系统进行灵活操作的便利interface。

          在实际应用中,可以通过使用File类对文件和目录进行管理和查找,提升程序的通用性和可移植性。

    转载地址:http://ekdoz.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>