Android文件管理器开发中对文件的复制,移动,删除,新建文件夹等的操作

//新建文件 public boolean newFile(File file) { boolean result = false; if (file != null) { try { result = file.createNewFile(); } catch (Exception e) { e.printStackTrace(); result = false; } } return result; } //删除文件 public boolean deleteFile(File file) { boolean result = false; if (file != null) { try { result = file.delete(); } catch (Exception e) { e.printStackTrace(); result = false; } } return result; } //删除文件夹 public boolean deleteFolder(File folder) { boolean result = false; try { String childs[] = folder.list(); if ( childs == null || childs.length <= 0 ) { if ( folder.delete() ) { result = true; } } else { for ( int i = 0; i < childs.length; i++ ) { String childName = childs[i]; String childPath = folder.getPath() + File.separator + childName; File filePath = new File(childPath); if (filePath.exists() && filePath.isFile()) { if (filePath.delete()) { result = true; } else { result = false; break; } } else if (filePath.exists() && filePath.isDirectory()) { if (deleteFolder(filePath)) { result = true; } else { result = false; break; } } } folder.delete(); } } catch (Exception e) { e.printStackTrace(); result = false; } return result; } //移动文件 public void moveFile(String source, String destination) { new File(source).renameTo( new File(destination) ); } //复制文件 private final static int BUFFER_LENGTH = 8192; public void copyFile( File src, File target ) { //Log.d( TAG, "copyFile: src = " + src.getAbsolutePath() + " target = " + target.getAbsolutePath() ); if( src.isDirectory() ) { copyDirectory(src, target); return; } InputStream in = null; OutputStream out = null; BufferedInputStream bin = null; BufferedOutputStream bout = null; try { in = new FileInputStream(src); out = new FileOutputStream(target); bin = new BufferedInputStream(in); bout = new BufferedOutputStream(out); byte[] b = new byte[BUFFER_LENGTH]; int len = bin.read(b); while ( len != -1 ) { bout.write(b, 0, len); len = bin.read(b); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } finally { try { if (bin != null) { bin.close(); } if (bout != null) { bout.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void copyDirectory( File src, File target ) { //Log.d( TAG, "copyDirectory: src = " + src.getAbsolutePath() + " target = " + target.getAbsolutePath() ); newFolder(target); if( src.isDirectory() ) { File [] files = src.listFiles(); if( files != null ) { for (File file : files) { File targetFile = new File(target.getAbsolutePath() + File.separator + file.getName()); if (file.isDirectory()) { newFolder(targetFile); copyDirectory(file, targetFile); } else { copyFile(file, targetFile); } } } } } //新建文件夹 public boolean newFolder( String file ) { File dirFile = new File( mCurDirectory.getAbsolutePath() + File.separator + file); try { if (!(dirFile.exists()) && !(dirFile.isDirectory())) { boolean ok = dirFile.mkdirs(); if ( ok ){ browseTo(mCurDirectory); return true; } else { return false; } } } catch (Exception e) { e.printStackTrace(); return false; } return true; } public boolean newFolder( File dirFile ) { try { if (!(dirFile.exists()) && !(dirFile.isDirectory())) { boolean ok = dirFile.mkdirs(); if ( ok ){ return true; } else { return false; } } } catch (Exception e) { e.printStackTrace(); return false; } return true; }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件管理器是一种常见的手机应用程序,用于浏览、管理和操作手机内部和外部存储上的文件文件夹。在Android操作系统,我们可以通过查看和分析文件管理器的源代码来了解其背后的实现原理。 文件管理器Android源码主要包含以下几个关键部分: 1. UI界面:文件管理器的UI界面由各种视图和控件构成,用于显示文件夹、文件以及其它相关信息,例如文件大小、文件类型和最后修改日期等。通过XML文件来定义这些视图和控件。 2. 文件系统API:Android提供了一系列的文件系统API,用于访问和操作文件文件夹。通过这些API,文件管理器可以实现文件复制移动删除、重命名等操作。此外,还可以获取文件的属性信息,例如文件大小、文件类型和最后修改日期等。 3. 目录结构及文件列表:文件管理器需要解析存储设备上的目录结构,并将其展示为文件列表。用户可以通过这个文件列表进行文件文件夹的选择和操作。 4. 文件操作功能:文件管理器提供了一系列的文件操作功能,例如新建文件夹复制文件移动文件删除文件、重命名文件等。这些操作功能通过调用文件系统API来实现。 5. 文件搜索:文件管理器还提供了文件搜索的功能。将用户输入的关键字与存储设备上的文件文件夹名称进行匹配,找到对应的文件文件夹,以满足用户的搜索需求。 总结起来,文件管理器Android源码主要包含UI界面的设计与实现、文件系统API的使用、目录结构的解析及文件列表的展示、文件操作功能的实现以及文件搜索功能的实现。通过分析源码,可以更深入地了解文件管理器的工作原理,同时也可以在此基础上进行二次开发和定制,以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值