• Android 文件操作


    CSDN 2011年度十大风云博客专栏评选火爆进行中! CSDN社区3月技术大分享百本IT图书等你拿!

    android的文件操作。(未整理完成)

    分类: 4android存储4697人阅读评论(16)收藏举报

    android的文件操作要有权限:

    1. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    SD卡下的文件操作:

    1、判断SD卡是否插入

    1. Environment.getExternalStorageState().equals(
    2. android.os.Environment.MEDIA_MOUNTED);

    2、获得sd卡根目录:

    1. File skRoot = Environment.getExternalStorageDirectory();

    私有目录下的文件操作:

    1、获得私有根目录:

    1. File fileRoot = Context.getFilesDir()+"//";

    还未整理

    文件夹或文件夹操作:

    1、确定或获得文件夹和文件路径

    a、获得文件或文件夹的绝对路径和相对路径。区别

    1. String path = File.getPath();//相对
    2. String path = File.getAbsoultePath();//绝对

    b 、获得文件或文件夹的父目录

    1. String parentPath = File.getParent();

    c、获得文件或文件夹的名称:

    1. String Name = File.getName();

    2、建立文件或文件夹

    1. File.mkDir(); //建立文件夹
    2. File.createNewFile();//建立文件

    3、判断是文件或文件夹

    1. File.isDirectory()

    4、列出文件夹下的所有文件和文件夹名

    1. File[] files = File.listFiles();

    5、修改文件夹和文件名

    1. File.renameTo(dest);

    6、删除文件夹或文件

    1. File.delete();

    1. package otheri.common;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8. import otheri.io.Input;
    9. import otheri.io.Output;
    10. import android.content.Context;
    11. import android.os.Environment;
    12. publicclass FileHelper {
    13. private Context context;
    14. private String SDPATH;
    15. private String FILESPATH;
    16. public FileHelper(Context context) {
    17. this.context = context;
    18. SDPATH = Environment.getExternalStorageDirectory().getPath() + "//";
    19. FILESPATH = this.context.getFilesDir().getPath() + "//";
    20. }
    21. /**
    22. * 在SD卡上创建文件
    23. *
    24. * @throws IOException
    25. */
    26. public File creatSDFile(String fileName) throws IOException {
    27. File file = new File(SDPATH + fileName);
    28. file.createNewFile();
    29. return file;
    30. }
    31. /**
    32. * 删除SD卡上的文件
    33. *
    34. * @param fileName
    35. */
    36. publicboolean delSDFile(String fileName) {
    37. File file = new File(SDPATH + fileName);
    38. if (file == null || !file.exists() || file.isDirectory())
    39. returnfalse;
    40. file.delete();
    41. returntrue;
    42. }
    43. /**
    44. * 在SD卡上创建目录
    45. *
    46. * @param dirName
    47. */
    48. public File creatSDDir(String dirName) {
    49. File dir = new File(SDPATH + dirName);
    50. dir.mkdir();
    51. return dir;
    52. }
    53. /**
    54. * 删除SD卡上的目录
    55. *
    56. * @param dirName
    57. */
    58. publicboolean delSDDir(String dirName) {
    59. File dir = new File(SDPATH + dirName);
    60. return delDir(dir);
    61. }
    62. /**
    63. * 修改SD卡上的文件或目录名
    64. *
    65. * @param fileName
    66. */
    67. publicboolean renameSDFile(String oldfileName, String newFileName) {
    68. File oleFile = new File(SDPATH + oldfileName);
    69. File newFile = new File(SDPATH + newFileName);
    70. return oleFile.renameTo(newFile);
    71. }
    72. /**
    73. * 拷贝SD卡上的单个文件
    74. *
    75. * @param path
    76. * @throws IOException
    77. */
    78. publicboolean copySDFileTo(String srcFileName, String destFileName)
    79. throws IOException {
    80. File srcFile = new File(SDPATH + srcFileName);
    81. File destFile = new File(SDPATH + destFileName);
    82. return copyFileTo(srcFile, destFile);
    83. }
    84. /**
    85. * 拷贝SD卡上指定目录的所有文件
    86. *
    87. * @param srcDirName
    88. * @param destDirName
    89. * @return
    90. * @throws IOException
    91. */
    92. publicboolean copySDFilesTo(String srcDirName, String destDirName)
    93. throws IOException {
    94. File srcDir = new File(SDPATH + srcDirName);
    95. File destDir = new File(SDPATH + destDirName);
    96. return copyFilesTo(srcDir, destDir);
    97. }
    98. /**
    99. * 移动SD卡上的单个文件
    100. *
    101. * @param srcFileName
    102. * @param destFileName
    103. * @return
    104. * @throws IOException
    105. */
    106. publicboolean moveSDFileTo(String srcFileName, String destFileName)
    107. throws IOException {
    108. File srcFile = new File(SDPATH + srcFileName);
    109. File destFile = new File(SDPATH + destFileName);
    110. return moveFileTo(srcFile, destFile);
    111. }
    112. /**
    113. * 移动SD卡上的指定目录的所有文件
    114. *
    115. * @param srcDirName
    116. * @param destDirName
    117. * @return
    118. * @throws IOException
    119. */
    120. publicboolean moveSDFilesTo(String srcDirName, String destDirName)
    121. throws IOException {
    122. File srcDir = new File(SDPATH + srcDirName);
    123. File destDir = new File(SDPATH + destDirName);
    124. return moveFilesTo(srcDir, destDir);
    125. }
    126. /*
    127. * 将文件写入sd卡。如:writeSDFile("test.txt");
    128. */
    129. public Output writeSDFile(String fileName) throws IOException {
    130. File file = new File(SDPATH + fileName);
    131. FileOutputStream fos = new FileOutputStream(file);
    132. returnnew Output(fos);
    133. }
    134. /*
    135. * 在原有文件上继续写文件。如:appendSDFile("test.txt");
    136. */
    137. public Output appendSDFile(String fileName) throws IOException {
    138. File file = new File(SDPATH + fileName);
    139. FileOutputStream fos = new FileOutputStream(file, true);
    140. returnnew Output(fos);
    141. }
    142. /*
    143. * 从SD卡读取文件。如:readSDFile("test.txt");
    144. */
    145. public Input readSDFile(String fileName) throws IOException {
    146. File file = new File(SDPATH + fileName);
    147. FileInputStream fis = new FileInputStream(file);
    148. returnnew Input(fis);
    149. }
    150. /**
    151. * 建立私有文件
    152. *
    153. * @param fileName
    154. * @return
    155. * @throws IOException
    156. */
    157. public File creatDataFile(String fileName) throws IOException {
    158. File file = new File(FILESPATH + fileName);
    159. file.createNewFile();
    160. return file;
    161. }
    162. /**
    163. * 建立私有目录
    164. *
    165. * @param dirName
    166. * @return
    167. */
    168. public File creatDataDir(String dirName) {
    169. File dir = new File(FILESPATH + dirName);
    170. dir.mkdir();
    171. return dir;
    172. }
    173. /**
    174. * 删除私有文件
    175. *
    176. * @param fileName
    177. * @return
    178. */
    179. publicboolean delDataFile(String fileName) {
    180. File file = new File(FILESPATH + fileName);
    181. return delFile(file);
    182. }
    183. /**
    184. * 删除私有目录
    185. *
    186. * @param dirName
    187. * @return
    188. */
    189. publicboolean delDataDir(String dirName) {
    190. File file = new File(FILESPATH + dirName);
    191. return delDir(file);
    192. }
    193. /**
    194. * 更改私有文件名
    195. *
    196. * @param oldName
    197. * @param newName
    198. * @return
    199. */
    200. publicboolean renameDataFile(String oldName, String newName) {
    201. File oldFile = new File(FILESPATH + oldName);
    202. File newFile = new File(FILESPATH + newName);
    203. return oldFile.renameTo(newFile);
    204. }
    205. /**
    206. * 在私有目录下进行文件复制
    207. *
    208. * @param srcFileName
    209. * : 包含路径及文件名
    210. * @param destFileName
    211. * @return
    212. * @throws IOException
    213. */
    214. publicboolean copyDataFileTo(String srcFileName, String destFileName)
    215. throws IOException {
    216. File srcFile = new File(FILESPATH + srcFileName);
    217. File destFile = new File(FILESPATH + destFileName);
    218. return copyFileTo(srcFile, destFile);
    219. }
    220. /**
    221. * 复制私有目录里指定目录的所有文件
    222. *
    223. * @param srcDirName
    224. * @param destDirName
    225. * @return
    226. * @throws IOException
    227. */
    228. publicboolean copyDataFilesTo(String srcDirName, String destDirName)
    229. throws IOException {
    230. File srcDir = new File(FILESPATH + srcDirName);
    231. File destDir = new File(FILESPATH + destDirName);
    232. return copyFilesTo(srcDir, destDir);
    233. }
    234. /**
    235. * 移动私有目录下的单个文件
    236. *
    237. * @param srcFileName
    238. * @param destFileName
    239. * @return
    240. * @throws IOException
    241. */
    242. publicboolean moveDataFileTo(String srcFileName, String destFileName)
    243. throws IOException {
    244. File srcFile = new File(FILESPATH + srcFileName);
    245. File destFile = new File(FILESPATH + destFileName);
    246. return moveFileTo(srcFile, destFile);
    247. }
    248. /**
    249. * 移动私有目录下的指定目录下的所有文件
    250. *
    251. * @param srcDirName
    252. * @param destDirName
    253. * @return
    254. * @throws IOException
    255. */
    256. publicboolean moveDataFilesTo(String srcDirName, String destDirName)
    257. throws IOException {
    258. File srcDir = new File(FILESPATH + srcDirName);
    259. File destDir = new File(FILESPATH + destDirName);
    260. return moveFilesTo(srcDir, destDir);
    261. }
    262. /*
    263. * 将文件写入应用私有的files目录。如:writeFile("test.txt");
    264. */
    265. public Output wirteFile(String fileName) throws IOException {
    266. OutputStream os = context.openFileOutput(fileName,
    267. Context.MODE_WORLD_WRITEABLE);
    268. returnnew Output(os);
    269. }
    270. /*
    271. * 在原有文件上继续写文件。如:appendFile("test.txt");
    272. */
    273. public Output appendFile(String fileName) throws IOException {
    274. OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);
    275. returnnew Output(os);
    276. }
    277. /*
    278. * 从应用的私有目录files读取文件。如:readFile("test.txt");
    279. */
    280. public Input readFile(String fileName) throws IOException {
    281. InputStream is = context.openFileInput(fileName);
    282. returnnew Input(is);
    283. }
    284. /**********************************************************************************************************/
    285. /*********************************************************************************************************/
    286. */
    287. /**
    288. * 删除一个文件
    289. *
    290. * @param file
    291. * @return
    292. */
    293. publicboolean delFile(File file) {
    294. if (file.isDirectory())
    295. returnfalse;
    296. return file.delete();
    297. }
    298. /**
    299. * 删除一个目录(可以是非空目录)
    300. *
    301. * @param dir
    302. */
    303. publicboolean delDir(File dir) {
    304. if (dir == null || !dir.exists() || dir.isFile()) {
    305. returnfalse;
    306. }
    307. for (File file : dir.listFiles()) {
    308. if (file.isFile()) {
    309. file.delete();
    310. } elseif (file.isDirectory()) {
    311. delDir(file);// 递归
    312. }
    313. }
    314. dir.delete();
    315. returntrue;
    316. }
    317. /**
    318. * 拷贝一个文件,srcFile源文件,destFile目标文件
    319. *
    320. * @param path
    321. * @throws IOException
    322. */
    323. publicboolean copyFileTo(File srcFile, File destFile) throws IOException {
    324. if (srcFile.isDirectory() || destFile.isDirectory())
    325. returnfalse;// 判断是否是文件
    326. FileInputStream fis = new FileInputStream(srcFile);
    327. FileOutputStream fos = new FileOutputStream(destFile);
    328. int readLen = 0;
    329. byte[] buf = newbyte[1024];
    330. while ((readLen = fis.read(buf)) != -1) {
    331. fos.write(buf, 0, readLen);
    332. }
    333. fos.flush();
    334. fos.close();
    335. fis.close();
    336. returntrue;
    337. }
    338. /**
    339. * 拷贝目录下的所有文件到指定目录
    340. *
    341. * @param srcDir
    342. * @param destDir
    343. * @return
    344. * @throws IOException
    345. */
    346. publicboolean copyFilesTo(File srcDir, File destDir) throws IOException {
    347. if (!srcDir.isDirectory() || !destDir.isDirectory())
    348. returnfalse;// 判断是否是目录
    349. if (!destDir.exists())
    350. returnfalse;// 判断目标目录是否存在
    351. File[] srcFiles = srcDir.listFiles();
    352. for (int i = 0; i < srcFiles.length; i++) {
    353. if (srcFiles[i].isFile()) {
    354. // 获得目标文件
    355. File destFile = new File(destDir.getPath() + "//"
    356. + srcFiles[i].getName());
    357. copyFileTo(srcFiles[i], destFile);
    358. } elseif (srcFiles[i].isDirectory()) {
    359. File theDestDir = new File(destDir.getPath() + "//"
    360. + srcFiles[i].getName());
    361. copyFilesTo(srcFiles[i], theDestDir);
    362. }
    363. }
    364. returntrue;
    365. }
    366. /**
    367. * 移动一个文件
    368. *
    369. * @param srcFile
    370. * @param destFile
    371. * @return
    372. * @throws IOException
    373. */
    374. publicboolean moveFileTo(File srcFile, File destFile) throws IOException {
    375. boolean iscopy = copyFileTo(srcFile, destFile);
    376. if (!iscopy)
    377. returnfalse;
    378. delFile(srcFile);
    379. returntrue;
    380. }
    381. /**
    382. * 移动目录下的所有文件到指定目录
    383. *
    384. * @param srcDir
    385. * @param destDir
    386. * @return
    387. * @throws IOException
    388. */
    389. publicboolean moveFilesTo(File srcDir, File destDir) throws IOException {
    390. if (!srcDir.isDirectory() || !destDir.isDirectory()) {
    391. returnfalse;
    392. }
    393. File[] srcDirFiles = srcDir.listFiles();
    394. for (int i = 0; i < srcDirFiles.length; i++) {
    395. if (srcDirFiles[i].isFile()) {
    396. File oneDestFile = new File(destDir.getPath() + "//"
    397. + srcDirFiles[i].getName());
    398. moveFileTo(srcDirFiles[i], oneDestFile);
    399. delFile(srcDirFiles[i]);
    400. } elseif (srcDirFiles[i].isDirectory()) {
    401. File oneDestFile = new File(destDir.getPath() + "//"
    402. + srcDirFiles[i].getName());
    403. moveFilesTo(srcDirFiles[i], oneDestFile);
    404. delDir(srcDirFiles[i]);
    405. }
    406. }
    407. returntrue;
    408. }
    409. }

    getPath与getAbsoultePath的区别:

    getAbsolutePath():返回抽象路径名的绝对路径名字符串。

    1. publicstaticvoid test1(){
    2. File file1 = new File(".//test1.txt");
    3. File file2 = new File("D://workspace//test//test1.txt");
    4. System.out.println("-----默认相对路径:取得路径不同------");
    5. System.out.println(file1.getPath());
    6. System.out.println(file1.getAbsolutePath());
    7. System.out.println("-----默认绝对路径:取得路径相同------");
    8. System.out.println(file2.getPath());
    9. System.out.println(file2.getAbsolutePath());
    10. }
    11. -----默认相对路径:取得路径不同------
    12. ./test1.txt
    13. D:/workspace/test/./test1.txt
    14. -----默认绝对路径:取得路径相同------
    15. D:/workspace/test/test1.txt
    16. D:/workspace/test/test1.txt
    17. ----------------------------------------------------
    18. publicstaticvoid test2() throws Exception{
    19. File file = new File("..//src//test1.txt");
    20. System.out.println(file.getAbsolutePath());
    21. System.out.println(file.getCanonicalPath());
    22. }
    23. D:/workspace/test/../src/test1.txt
    24. D:/workspace/src/test1.txt
    25. --------------------------------------------
    26. publicstaticvoid test3() throws Exception{
    27. File file = new File("D://Text.txt");
    28. System.out.println(file.getCanonicalPath());

    (1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
    D:/Text.txt注意这里试大写的Text.txt
    (2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
    D:/text.txt同样的代码得到不同的结果。

  • 相关阅读:
    Case用法
    SQL 常用函数及示例
    ms-SQL 递归调用
    office2003 下载地址 及密码
    [LeetCode] 697. Degree of an Array 数组的度
    [LeetCode] 58. Length of Last Word 求末尾单词的长度
    [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
    [LeetCode] 287. Find the Duplicate Number 寻找重复数
    [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II
    [LeetCode] 349. Intersection of Two Arrays 两个数组相交
  • 原文地址:https://www.cnblogs.com/shanshan520/p/2424037.html
Copyright © 2020-2023  润新知