您的位置:首页 > 移动开发 > Android开发

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

2016-10-19 09:27 597 查看
//新建文件 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; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐