首页 > 其他分享 >Android开发 - File类文件操作解析

Android开发 - File类文件操作解析

时间:2024-08-13 13:39:36浏览次数:7  
标签:解析 File sdcard file new Android txt example

File 是什么

  • File 类用于处理文件和目录。它允许你创建删除读取写入文件。你可以用它来获取文件路径检查文件是否存在获取文件大小等。例如,File file = new File(context.getFilesDir(), "example.txt"); 可以用来在应用的私有目录中创建一个名为 example.txt 的文件

File 构造方法

  • File(String pathname):用一个路径字符串创建一个 File 对象

    File file = new File("/sdcard/example.txt");
    
    • 参数解析
      • pathname文件或目录的完整路径,比如 /sdcard/example.txt
  • File(String parent, String child):用一个父目录路径和一个文件名创建 File 对象

    File file = new File("/sdcard", "example.txt");
    
    • 参数解析
      • parent:父目录路径,比如 /sdcard
      • child:文件或子目录的名字,比如 example.txt
  • File(File parent, String child):用一个父 File 对象和一个文件名创建 File 对象

    File parentDir = new File("/sdcard");
    File file = new File(parentDir, "example.txt");
    
    • 参数解析

      • parent:表示父目录的 File 对象,比如 new File("/sdcard")

      • child文件名子目录名,比如 example.txt

  • File(URI uri):用一个 URI 对象创建 File 对象

    URI uri = new URI("file:///sdcard/example.txt");
    File file = new File(uri);
    
    • 参数解析
      • uri: 统一资源标识符,描述文件的路径,如 file:///sdcard/example.txt

File 主要方法

  • file.exists():检查文件目录是否存在

    File file = new File("/sdcard/example.txt");
    if (file.exists()) {
        // 文件存在
    }
    
  • file.createNewFile()创建新文件(如果它不存在)

    File file = new File("/sdcard/example.txt");
    if (file.createNewFile()) {
        // 文件创建成功
    }
    
  • file.delete()删除文件或空目录

    File file = new File("/sdcard/example.txt");
    if (file.delete()) {
        // 文件删除成功
    }
    
  • file.length()获取文件的大小(字节数)

    File file = new File("/sdcard/example.txt");
    long size = file.length();
    
  • oldFile.renameTo(File dest)重命名文件或移动到新位置

    File oldFile = new File("/sdcard/oldname.txt");
    File newFile = new File("/sdcard/newname.txt");
    if (oldFile.renameTo(newFile)) {
        // 文件重命名成功
    }
    
    • 参数解析
      • dest新文件或新位置
  • files.listFiles()列出目录中的所有文件和子目录

    File dir = new File("/sdcard/mydir");
    File[] files = dir.listFiles();
    if (files != null) {
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
    

构造方法总结

  • File(String pathname)直接通过路径字符串创建文件对象

  • File(String parent, String child)通过父路径和子路径创建文件对象

  • File(File parent, String child)通过父 File 对象和子路径创建文件对象

  • File(URI uri)通过 URI 创建文件对象,适用于处理网络或资源路径

标签:解析,File,sdcard,file,new,Android,txt,example
From: https://www.cnblogs.com/ajunjava/p/18356683

相关文章

  • uniapp中如何使用uni.canvasToTempFilePath方法上传Canvas内容为图片,并理解其工作原理
    1.主函数uni.canvasToTempFilePath({ canvasId:'canvasid', fileType:'png', quality:1,//图片质量 success(res){ uni.uploadFile({ url:that.baseUrl+'/file/upload',//后端接口地址 name:'file&......
  • 【图像去噪】论文复现:新手入门必看!DnCNN的Pytorch源码训练测试全流程解析!为源码做详细
    第一次来请先看【专栏介绍文章】:源码只提供了noiselevel为25的DnCNN-S模型文件。本文末尾有完整代码和训练好的σ=15,25,50的DnCNN-S、σ∈[0,55]的DnCNN-B和CDnCNN-B、DnCNN-3共6个模型文件!读者可以自行下载!本文亮点:以官方Pytorch源代码为基础,在DnCNN-S的基础上,增添Dn......
  • OFtutorial06_customClasses 解析
    组成如图customClass.H#include"fvCFD.H"classcustomClass{private:labelmyInt_;public:customClass();~customClass();//Accessoperators-allowtheinternalvaluetobesetorretrieved.//Definedasinlinetomake......
  • Makefile 编译多级目录多个目标文件模板
    对于当前目录结构下的Makefile(基于图书管理系统).├──Makefile├──README.md├──bin│├──adminsys│└──usersys├──build│├──adminmain.o│├──generalcore.o│├──generalimpl.o│├──generalview.o│├──......
  • 深入解析Node.js中的fs.watch:options与listener详解
    在Node.js中,fs.watch方法是一个功能强大的文件系统监控工具,它允许我们对文件或目录进行实时监控,并在文件或目录发生变化时触发相应的操作。在使用fs.watch时,两个关键的部分是options对象和listener回调函数。本文将详细讲解这两个部分,帮助读者更好地理解和使用fs.watch。一......
  • Java基础入门18:File、IO 流1(方法递归、字符集、IO流-字节流)
    File和IO流FileFile是java.io.包下的类,File类的对象,用于代表当前操作系统的文件(可以是文件、或文件夹)。IO流用于读写数据的(可以读写文件,或网络中的数据...)File代表文件IO流用来读写数据File创建对象创建File类的对象注意:File对象既可以代表文件、也可以代表文......
  • Android之集成Unity及互相调用
    Unity官方文档DemoUnity与原生交互之AndroidStudio篇——Unity导出Android工程,导入AndroidStudio打包APK全流程Unity3D与Android交互问题一Error:Unity.IL2CPP.Building.BuilderFailedException:Buildfailedwith0successfulnodesand0failedonesError:Inte......
  • Android KTX
    AndroidKTX是包含在Android Jetpack 及其他Android库中的一组Kotlin扩展程序。KTX扩展程序可以为Jetpack、Android平台及其他API提供简洁的惯用Kotlin代码。为此,这些扩展程序利用了多种Kotlin语言功能,其中包括:扩展函数扩展属性Lambda命名参数参数默认值......
  • GUI推送安装报错:cannot create regular file
    EnvironmentDataProtector10.60RedHatEnterpriseLinux8.3 SituationThefollowingerrorisreportedwhentheAddclientsisperformedontheDPGUI. [Critical]<Client-hostname>Certificatecopyfailed:cp:cannotcreateregularfile'/etc......
  • 如何解决因内存不足导致的 iPhone 白苹果问题:原因解析与修复教程
    引言在使用iPhone的过程中,许多用户可能遇到过“白苹果”现象,即设备启动时只显示白色的Apple标志,却无法进入系统。这种情况通常让人感到困惑和担忧,尤其是当重要数据似乎无法访问时。本文将探讨内存不足导致iPhone白苹果的原因,并提供详细的修复教程,帮助你恢复设备正常运行......