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; }
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; }
相关文章推荐
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- 纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- ( 转载)纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- PHP操作文件类(文件和文件夹创建,复制,移动和删除)
- PHP操作文件类的函数代码(文件和文件夹创建,复制,移动和删除)
- PHP操作文件类(文件和文件夹创建,复制,移动和删除)
- java操作文件工具类( 新建、复制、删除、移动文件(夹))
- java中对文件file的操作 新建文件 移动文件 复制文件 删除文件
- 文件以及文件夹处理(新建,移动,删除,复制)
- C# 对文件与文件夹的操作 -- 删除、移动与复制
- php文件及文件夹操作(创建、删除、移动、复制)
- php(创建、删除、移动、复制)文件及文件夹操作的实现方法
- linux学习入门3——linux文件基本操作(linuxcast.net)(复制、移动、重命名、创建、删除文件及文件夹)
- PHP文件及文件夹操作(创建,删除,移动,复制)
- 纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- 纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- PHP操作文件类(文件和文件夹创建,复制,移动和删除)
- 文件操作 复制移动删除重命名文件文件夹(转帖)
- 文件操作 复制移动删除重命名文件文件夹(转帖)
- java 对文件操作 文件(夹)新建、复制、删除、移动