中如何linux中复制文件夹和移动文件及文件夹

点击这里,将文章分享到自己的动态
复制一个文件夹里的文件到另一个文件夹 (使用递归的方法实现)
最近在由于准备实习,在学android,也会用到Java,所以想实现复制文件夹的功能,当然也参考了别人的代码。这里是我参考的网址:
我发这个文章的主要目的是为了永久性保存我的这个小小成功,虽然参考了不少,但是我搞明白了原理。下一篇用队列的方式又写了一个源文件夹里的所有文件到一个目标目录下的代码,这个目标目录下只有文件,没有文件夹。
import java.io.BufferedInputS
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileOutputS
import java.io.IOE
public class CopyFile {
//public class MainActivity extends Activity {
//Button copyB
static String
copy_From ="E:\\amu1";//源文件
static String
copy_TO="E:\\amu2";
//目标文件
public static void main(String args[]) throws IOException {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
copyButton=(Button) findViewById(R.id.button1);
copyButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*如果SD卡已经插入,则正常运行
* 否则抛出错误
(new File(copy_TO)).mkdirs(); // 不存在就新建目标文件
File [] get_From=(new File(copy_From)).listFiles();
//获取源文件中的文件或目录
for (int i=0;i&get_From.i++) {
if(get_From[i].isFile()){
//调用复制文件的函数
String toFile = copy_TO +"/"+ get_From[i].getName();
//写的时候这里少了个/.导致不能复制
copy_File(get_From[i],new File (toFile));
if(get_From[i].isDirectory()){
//复制文件的目录
String from_Directory=copy_From+File.separator+get_From[i].getName();
String to_Directory=copy_TO+File.separator+get_From[i].getName();
copy_Directory(from_Directory,to_Directory);//调用复制文件夹的函数
private static void copy_Directory(String fromDirectory, String toDirectory)throws IOException {
//if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// TODO Auto-generated method stub
//新建目标文件
new File(toDirectory).mkdirs();
//获取文件夹或者文件目录
File[] files=(new File(fromDirectory)).listFiles();
for(int i=0;i&files.i++){
if(files[i].isFile()){
//如果是文件
File fromFile=files[i];
File toFile=new File(new File(toDirectory).getAbsolutePath()+File.separator+files[i].getName());
copy_File(fromFile, toFile);
if(files[i].isDirectory()){
//如果是文件夹
fromDir=fromDirectory+"/"+files[i].getName();
toDir=toDirectory+"/"+files[i].getName();
copy_Directory(fromDir, toDir);
//Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
//Toast.makeText(this, "复制失败", Toast.LENGTH_LONG).show();
private static void copy_File(File from_File, File to_File) throws IOException {
//if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// TODO Auto-generated method stub
FileInputStream infile=new FileInputStream(from_File);//新建输入流
BufferedInputStream inbuf=new BufferedInputStream(infile);//对输入流进行缓冲
FileOutputStream outfile=new FileOutputStream(to_File);
//新建输出流
BufferedOutputStream outbuf=new BufferedOutputStream(outfile);//对输出流进行缓冲
//缓冲数组
bt=new byte[2048];
((len=inbuf.read(bt))!=-1) {
outbuf.write(bt, 0, len);
outbuf.flush();
//刷新缓冲
infile.close();
inbuf.close();
outfile.close();
outbuf.close();
//Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
//Toast.makeText(this, "复制失败", Toast.LENGTH_LONG).show();
//Toast.makeText(this, "复制失败", Toast.LENGTH_LONG).show();
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
若觉得本文不错,就分享一下吧!
评论加载中...
看过此文的用户,还看了以下文章
正在加载中
作者相关文章- 系统光盘下载网站!
当前位置: >
> 详细页面
Win7系统怎么快速复制文件或文件夹路径?
来源:系统之家
作者:lizhu
  最近有很多Win7系统用户询问,当我们要复制一个文件或文件夹的路径除了在资源管理器中复制路径+文件名这个方法外,有没有更加简单,快速的方法。下面,我们一起来看看。
  操作方法:
  1、按住 SHIFT键 然后对文件或文件夹点击右键,我们就可以看到多了一项&复制到路径&选项。
  2、只要点击&复制到路径&,路径就复制到剪贴板中了!就可以粘贴了。
  以上就是快速复制文件或文件夹路径的方法了,如果你觉得在资源管理器中复制路径+文件名这个方法麻烦的话,不妨试试小编推荐的这个。
栏目热门教程
人气教程排行
热门系统下载
本站发布的系统与软件仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件!如侵犯到您的权益,请及时通知我们,我们会及时处理。
Copyright&2011 系统之家(www.xitongzhijia.net) 版权所有 闽ICP备号-1复制和移动文件和文件夹
我的图书馆
复制和移动文件和文件夹
[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢下次自动登录
现在的位置:
& 综合 & 正文
复制、删除和移动文件和文件夹
1. 复制文件和目录:
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:/Users/Public/TestFolder
// C:/Users/Public/TestFolder/test.txt
// C:/Users/Public/TestFolder/SubDir/test.txt
public class SimpleFileCopy
static void Main()
string fileName = "test.txt";
string sourcePath = @"C:/Users/Public/TestFolder";
string targetPath =
@"C:/Users/Public/TestFolder/SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath);
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
in this code example.
if (System.IO.Directory.Exists(sourcePath))
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
Console.WriteLine("Source path does not exist!");
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
2. 移动文件和目录:
// Simple synchronous file move operations with no user interface.
public class SimpleFileMove
static void Main()
string sourceFile = @"C:/Users/Public/public/test.txt";
string destinationFile = @"C:/Users/Public/private/test.txt";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
// To move an entire directory. To programmatically modify or combine
// path strings, use the System.IO.Path class.
System.IO.Directory.Move(@"C:/Users/Public/public/test/", @"C:/Users/Public/private");
3. 删除文件和目录:
// Simple synchronous file deletion operations with no user interface.
// To run this sample, create the following files on your drive:
// C:/Users/Public/DeleteTest/test1.txt
// C:/Users/Public/DeleteTest/test2.txt
// C:/Users/Public/DeleteTest/SubDir/test2.txt
public class SimpleFileDelete
static void Main()
// Delete a file by using File class static method...
if(System.IO.File.Exists(@"C:/Users/Public/DeleteTest/test.txt"))
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
System.IO.File.Delete(@"C:/Users/Public/DeleteTest/test.txt");
catch (System.IO.IOException e)
Console.WriteLine(e.Message);
// ...or by using FileInfo instance method.
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Users/Public/DeleteTest/test2.txt");
fi.Delete();
catch (System.IO.IOException e)
Console.WriteLine(e.Message);
// Delete a directory. Must be writable or empty.
System.IO.Directory.Delete(@"C:/Users/Public/DeleteTest");
catch (System.IO.IOException e)
Console.WriteLine(e.Message);
// Delete a directory and all subdirectories with Directory static method...
if(System.IO.Directory.Exists(@"C:/Users/Public/DeleteTest"))
System.IO.Directory.Delete(@"C:/Users/Public/DeleteTest", true);
catch (System.IO.IOException e)
Console.WriteLine(e.Message);
// ...or with DirectoryInfo instance method.
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:/Users/Public/public");
// Delete this dir and all subdirs.
di.Delete(true);
catch (System.IO.IOException e)
Console.WriteLine(e.Message);
【上篇】【下篇】

我要回帖

更多关于 java中复制文件夹 的文章

 

随机推荐