Android文件操作

本文详细介绍了Android系统中进行文件操作的步骤和方法,包括获取SD卡和私有目录路径,创建、删除、重命名文件及目录,以及读写文件等操作。确保使用前获取必要的权限,例如`<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>`。
摘要由CSDN通过智能技术生成
  1. android的文件操作要有权限:
  2. view plaincopy to clipboardprint?
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  3. SD卡下的文件操作:
  4. 1、判断SD卡是否插入
  5. view plaincopy to clipboardprint?
    Environment.getExternalStorageState().equals(  
            android.os.Environment.MEDIA_MOUNTED); 
    Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED);
  6. 2、获得sd卡根目录:
  7. view plaincopy to clipboardprint?
    File skRoot = Environment.getExternalStorageDirectory(); 
    File skRoot = Environment.getExternalStorageDirectory();
  8. 私有目录下的文件操作:
  9. 1、获得私有根目录:
  10. view plaincopy to clipboardprint?
    File fileRoot = Context.getFilesDir()+"\"; 
     File fileRoot = Context.getFilesDir()+"\";
  11. 还未整理
  12. 文件夹或文件夹操作:
  13. 1、确定或获得文件夹和文件路径
  14. a、获得文件或文件夹的绝对路径和相对路径。区别
  15. view plaincopy to clipboardprint?
    String path = File.getPath();//相对  
    String path = File.getAbsoultePath();//绝对 
    String path = File.getPath();//相对
    String path = File.getAbsoultePath();//绝对
  16. b 、获得文件或文件夹的父目录
  17. view plaincopy to clipboardprint?
    String parentPath = File.getParent(); 
    String parentPath = File.getParent();
  18. c、获得文件或文件夹的名称:
  19. view plaincopy to clipboardprint?
    String Name = File.getName(); 
    String Name = File.getName();
  20. 2、建立文件或文件夹
  21. view plaincopy to clipboardprint?
    File.mkDir(); //建立文件夹  
    File.createNewFile();//建立文件 
    File.mkDir(); //建立文件夹
    File.createNewFile();//建立文件
     
  22. 3、判断是文件或文件夹
  23. view plaincopy to clipboardprint?
    File.isDirectory() 
    File.isDirectory()
  24. 4、列出文件夹下的所有文件和文件夹名
  25. view plaincopy to clipboardprint?
    File[] files = File.listFiles(); 
    File[] files = File.listFiles();
  26. 5、修改文件夹和文件名
  27. view plaincopy to clipboardprint?
    File.renameTo(dest); 
    File.renameTo(dest);
  28. 6、删除文件夹或文件
  29. view plaincopy to clipboardprint?
    File.delete(); 
    File.delete();
  30. view plaincopy to clipboardprint?
    package otheri.common;  
     
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
     
    import otheri.io.Input;  
    import otheri.io.Output;  
    import android.content.Context;  
    import android.os.Environment;  
     
    public class FileHelper {  
        private Context context;  
        private String SDPATH;  
        private String FILESPATH;  
     
        public FileHelper(Context context) {  
            this.context = context;  
            SDPATH = Environment.getExternalStorageDirectory().getPath() + "\";  
            FILESPATH = this.context.getFilesDir().getPath() + "\";  
        }  
     
         
        public File creatSDFile(String fileName) throws IOException {  
            File file = new File(SDPATH + fileName);  
            file.createNewFile();  
            return file;  
        }  
     
         
        public boolean delSDFile(String fileName) {  
            File file = new File(SDPATH + fileName);  
            if (file == null || !file.exists() || file.isDirectory())  
                return false;  
            file.delete();  
            return true;  
        }  
     
         
        public File creatSDDir(String dirName) {  
            File dir = new File(SDPATH + dirName);  
            dir.mkdir();  
            return dir;  
        }  
     
         
        public boolean delSDDir(String dirName) {  
            File dir = new File(SDPATH + dirName);  
            return delDir(dir);  
        }  
     
         
        public boolean renameSDFile(String oldfileName, String newFileName) {  
            File oleFile = new File(SDPATH + oldfileName);  
            File newFile = new File(SDPATH + newFileName);  
            return oleFile.renameTo(newFile);  
        }  
     
         
        public boolean copySDFileTo(String srcFileName, String destFileName)  
                throws IOException {  
            File srcFile = new File(SDPATH + srcFileName);  
            File destFile = new File(SDPATH + destFileName);  
            return copyFileTo(srcFile, destFile);  
        }  
     
         
        public boolean copySDFilesTo(String srcDirName, String destDirName)  
                throws IOException {  
            File srcDir = new File(SDPATH + srcDirName);  
            File destDir = new File
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值