黑马程序员技术交流社区

标题: 写一个程序 [打印本页]

作者: 殷士剑    时间: 2012-11-17 16:46
标题: 写一个程序
本帖最后由 殷士剑 于 2012-11-18 20:16 编辑

把当前文件夹中的 N 个文件夹中的avi视频剪切到当前文件中的一个新的文件夹里,用java怎样实现?
作者: 王阳    时间: 2012-11-17 16:56
思路应该和copy文件然后删除差不多,如果是多个文件夹,那就多次循环遍历就可以了。当copy一个avi结束后,然后删除。
作者: 葛旭东    时间: 2012-11-17 17:13
本帖最后由 葛旭东 于 2012-11-17 17:43 编辑
  1. import java.io.File;
  2. //遍历文件夹,用File的renameTo(File dest)方法移到新文件夹中。

  3. public class CutDemo {
  4.         public static void main(String[] args) {
  5.                 File f1 = new File("e:\\柯南");
  6.                 File f2 = new File("e:\\My File");
  7.                 f2.mkdir();
  8.                 cut(f1,f2);
  9.         }
  10.     public static void cut(File f1,File f2)
  11.     {
  12.                 File[] files = f1.listFiles();
  13.                 for(File file:files)
  14.                 {
  15.                         if(file.isDirectory())
  16.                                 cut(file,f2);
  17.                         else
  18.                                 {
  19.                                 String str = file.getName();                               
  20.                                 if(str.endsWith(".mp4"))                               
  21.                                 {                       
  22.                                         file.renameTo(new File(f2,str));
  23.                                 }
  24.                                 }
  25.                 }
  26.     }
  27. }
复制代码

作者: 郝少普    时间: 2012-11-17 19:21
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
private static FileInputStream fis = null;
private static FileOutputStream fos = null;
private static BufferedInputStream bis = null;
private static BufferedOutputStream bos = null;

public static void main(String[] args) {
  copy("D:\\*.avi", "C:\\*.avi");   // 传入两个地址  第一个为要拷贝的avi,  第二个为要输出的地址
}

public static void copy(String c_url,String p_url){
  try {
   fis = new FileInputStream(c_url);
   bis = new BufferedInputStream(fis);
   fos = new FileOutputStream(p_url);
   bos = new BufferedOutputStream(fos);
   int b = 0;
   while((b=bis.read())>0){
    bos.write(b);
    bos.flush();
   }
   System.out.println("copy success!");
   
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}
}


感觉要是剪辑的话实现起来不容易,暂时没有很好的思路,只实现的文件的拷贝。 哎, 技术有限

作者: 聽聽我dē❤    时间: 2012-11-17 20:02
/*
文件拷贝
*/
import java.io.*;
class CoppyDemo
{
        public static void main(String[] args)
        {       
               
                copy_method("D:\\1.mp3","E:\\2.mp3");       

        }
        public static void copy_method(String fu,String zi )
        {       
                BufferedInputStream fis=null;
                BufferedOutputStream fos=null;
                try
                {
                        fis=new BufferedInputStream(new FileInputStream(fu));               
                        fos=new BufferedOutputStream(new FileOutputStream(zi));
                        int by=0;
                        while ((by=fis.read())!=-1)
                        {
                        fos.write(by);
                        }
                }
                catch (IOException e)
                {
                        throw new RuntimeException("复制文件错误");
                }
                finally
                {
                        try
                        {
                                if(fis!=null)
                                        fis.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("读取关闭失败");
                        }
                        try
                        {
                                if(fos!=null)
                                        fos.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("写入关闭失败");
                        }
        }               
        }

}
看看应该可以的,自己把文件格式改一下就行了。
作者: ssx0101    时间: 2012-11-18 16:25
  1. package pt;

  2. import java.io.*;
  3. public class SC {

  4.         /**
  5.          * @param args
  6.          * @throws IOException
  7.          * 复制后删掉源文件就是剪切了。
  8.          * 我写的这个可以往其他文件夹里剪切文件,
  9.          * 把file和file_1写成同一个文件夹就是楼主的要求
  10.          */
  11.         public static void main(String[] args) throws IOException {
  12.                 File file=new File("F:\\mytest");
  13.                 File file_1=new File("E:\\Copy_mytest");
  14.                 if(!file_1.exists())
  15.                         file_1.mkdirs();
  16.                 find(file,file_1);
  17.         }
  18.         public static void find(File s,File o) throws IOException{
  19.                 File[] files=s.listFiles();
  20.                 BufferedOutputStream bos=null;
  21.                 BufferedInputStream bis=null;
  22.                 for(int i=0;i<files.length;i++){
  23.                         byte[] buf=new byte[1024*1024*50];//设置能移动的文件大小最大为50M,因为java虚拟机的内存默认大小是64M。
  24.                         int len=0;
  25.                         if(!files[i].isHidden()&&files[i].isDirectory())//如果是文件夹,继续找
  26.                         {
  27.                                 find(files[i],o);
  28.                         }
  29.                         else
  30.                         {
  31.                                 if(files[i].getName().endsWith(".avi"))//找到avi文件
  32.                                 {
  33.                                         bis=new BufferedInputStream(new FileInputStream(files[i]));//把源文件夹内的文件或文件夹加入输入流
  34.                                         while((len=bis.read(buf))!=-1)
  35.                                         {
  36.                                                 File f=new File(o.getAbsolutePath()+"\\"+files[i].getName());//由目的文件夹名和被剪切文件名确定新文件的绝对路径
  37.                                                 if(f.exists())//如果重名,删掉旧的
  38.                                                         f.delete();
  39.                                                 f.createNewFile();
  40.                                             bos=new BufferedOutputStream(new FileOutputStream(f));//把新文件加入输出流
  41.                                                 bos.write(buf, 0, len);
  42.                                                 bos.flush();
  43.                                                 if(bos!=null)
  44.                                                         bos.close();
  45.                                         }
  46.                                         if(bis!=null)
  47.                                                 bis.close();
  48.                                         if(files[i].exists())//删掉源文件,完成剪切动作
  49.                                                 files[i].delete();
  50.                                 }
  51.                         }
  52.                 }
  53.         }

  54. }
复制代码
注意:
本程序能剪切的文件在50M以下,因为java虚拟机的内存默认很小。

作者: 殷士剑    时间: 2012-11-18 20:15
谢谢大家!
作者: ssx0101    时间: 2012-11-18 20:31
本帖最后由 曹自祥 于 2012-11-18 20:42 编辑

我后来想想应该可以解除剪切文件有大小限制的约束,对代码进行了小改动,现在可以剪切任意大小的文件了。
  1. package pt;

  2. import java.io.*;
  3. public class SC {

  4.         /**
  5.          * @param args
  6.          * @throws IOException
  7.          * 复制后删掉源文件就是剪切了。
  8.          * 我写的这个可以往其他文件夹里剪切文件,
  9.          * 把file和file_1写成同一个文件夹就是楼主的要求
  10.          */
  11.         public static void main(String[] args) throws IOException {
  12.                 File file=new File("F:\\mytest");
  13.                 File file_1=new File("E:\\Copy_mytest");
  14.                 if(!file_1.exists())
  15.                         file_1.mkdirs();
  16.                 find(file,file_1);
  17.         }
  18.         public static void find(File s,File o) throws IOException{
  19.                 File[] files=s.listFiles();
  20.                 BufferedOutputStream bos=null;
  21.                 BufferedInputStream bis=null;
  22.                 for(int i=0;i<files.length;i++){
  23.                         byte[] buf=new byte[1024*1024];
  24.                         int len=0;
  25.                         if(!files[i].isHidden()&&files[i].isDirectory())//如果是文件夹,继续往里找
  26.                         {
  27.                                 find(files[i],o);
  28.                         }
  29.                         else
  30.                         {
  31.                                 if(files[i].getName().endsWith(".avi"))//找到avi文件
  32.                                 {
  33.                                         bis=new BufferedInputStream(new FileInputStream(files[i]));//把源文件夹内的文件或文件夹关联输入流
  34.                                         File f=new File(o.getAbsolutePath()+"\\"+files[i].getName());//由目的文件夹名和被剪切文件名确定新文件的绝对路径
  35.                                         if(f.exists())//如果重名,删掉旧的
  36.                                                 f.delete();
  37.                                         f.createNewFile();
  38.                                     bos=new BufferedOutputStream(new FileOutputStream(f));//把新文件关联入输出流
  39.                                         while((len=bis.read(buf))!=-1)
  40.                                         {
  41.                                                 bos.write(buf, 0, len);
  42.                                         }
  43.                                         bos.flush();
  44.                                         if(bos!=null)
  45.                                                 bos.close();
  46.                                         if(bis!=null)
  47.                                                 bis.close();
  48.                                         if(files[i].exists())//删掉源文件,完成剪切动作
  49.                                                 files[i].delete();
  50.                                 }
  51.                         }
  52.                 }
  53.         }

  54. }
复制代码
剪切大文件需要的时间较长,需耐心等待
我切了个700M的文件用了半分多钟,切个1.3G的用了好几分钟。切1.3G的时候看了两分钟没切完就没看了,但程序还在运行,过一会儿再看竟然切完了。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2