android的文件、目录操作(三)

本文介绍如何在Android设备的SD卡上进行文件和目录的复制、移动以及文件的写入、追加读取等操作,包括创建私有文件、目录的方法。
 

android的文件、目录操作(三)  

2011-03-18 18:02:26|  分类: 工作学习 |  标签: |字号 订阅


  1.     /** 
  2.      * 拷贝SD卡上指定目录的所有文件 
  3.      *  
  4.      * @param srcDirName 
  5.      * @param destDirName 
  6.      * @return 
  7.      * @throws IOException 
  8.      */  
  9.     public boolean copySDFilesTo(String srcDirName, String destDirName)  
  10.             throws IOException {  
  11.         File srcDir = new File(SDPATH + srcDirName);  
  12.         File destDir = new File(SDPATH + destDirName);  
  13.         return copyFilesTo(srcDir, destDir);  
  14.     }  
  15.   
  16.     /** 
  17.      * 移动SD卡上的单个文件 
  18.      *  
  19.      * @param srcFileName 
  20.      * @param destFileName 
  21.      * @return 
  22.      * @throws IOException 
  23.      */  
  24.     public boolean moveSDFileTo(String srcFileName, String destFileName)  
  25.             throws IOException {  
  26.         File srcFile = new File(SDPATH + srcFileName);  
  27.         File destFile = new File(SDPATH + destFileName);  
  28.         return moveFileTo(srcFile, destFile);  
  29.     }  
  30.   
  31.     /** 
  32.      * 移动SD卡上的指定目录的所有文件 
  33.      *  
  34.      * @param srcDirName 
  35.      * @param destDirName 
  36.      * @return 
  37.      * @throws IOException 
  38.      */  
  39.     public boolean moveSDFilesTo(String srcDirName, String destDirName)  
  40.             throws IOException {  
  41.         File srcDir = new File(SDPATH + srcDirName);  
  42.         File destDir = new File(SDPATH + destDirName);  
  43.         return moveFilesTo(srcDir, destDir);  
  44.     }  
  45.   
  46.   
  47.     /* 
  48.      * 将文件写入sd卡。如:writeSDFile("test.txt"); 
  49.      */  
  50.     public Output writeSDFile(String fileName) throws IOException {  
  51.         File file = new File(SDPATH + fileName);  
  52.         FileOutputStream fos = new FileOutputStream(file);  
  53.         return new Output(fos);  
  54.     }  
  55.   
  56.     /* 
  57.      * 在原有文件上继续写文件。如:appendSDFile("test.txt"); 
  58.      */  
  59.     public Output appendSDFile(String fileName) throws IOException {  
  60.         File file = new File(SDPATH + fileName);  
  61.         FileOutputStream fos = new FileOutputStream(file, true);  
  62.         return new Output(fos);  
  63.     }  
  64.   
  65.     /* 
  66.      * 从SD卡读取文件。如:readSDFile("test.txt"); 
  67.      */  
  68.     public Input readSDFile(String fileName) throws IOException {  
  69.         File file = new File(SDPATH + fileName);  
  70.         FileInputStream fis = new FileInputStream(file);  
  71.         return new Input(fis);  
  72.     }  
  73.       
  74.       
  75.     /** 
  76.      * 建立私有文件 
  77.      *  
  78.      * @param fileName 
  79.      * @return 
  80.      * @throws IOException 
  81.      */  
  82.     public File creatDataFile(String fileName) throws IOException {  
  83.         File file = new File(FILESPATH + fileName);  
  84.         file.createNewFile();  
  85.         return file;  
  86.     }  
  87.   
  88.     /** 
  89.      * 建立私有目录 
  90.      *  
  91.      * @param dirName 
  92.      * @return 
  93.      */  
  94.     public File creatDataDir(String dirName) {  
  95.         File dir = new File(FILESPATH + dirName);  
  96.         dir.mkdir();  
  97.         return dir;  
  98.     }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值