FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("D:\\SOP_DEV\\apache-tomcat-7.0.27.rar"));
fos = new FileOutputStream(new File("apache-tomcat-7.0.27.rar"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

// 按照一个字节一个字节的读取
int tempByte;
try {
while ((tempByte = fis.read()) != -1) {
fos.write(tempByte);
}
} catch (IOException e) {
e.printStackTrace();
}

try {
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}文件类型拿到D:\\SOP_DEV\\apache-tomcat-7.0.27.rar 通过 . 分隔,看文件后缀是否满足格式要求就可以了

解决方案 »

  1.   

    没有完整代码,说下思路。
    1、可以用java的NIO包中提供的ByteBuffer为主要媒介(位于输入流和输出流之间);
    2、关于楼主的“设定可复制的文件类型”这个没太理解,如果用字节流实现的话,和文件类型无关了;
    3、由于复制过程中其它条件并没有说明,可能有以下几点需要注意:
        a、文件访问控制的问题。比如可能在复制过程中有其它进程需要访问同一个文件;
        b、复制中文件变化了。比如其它进程向文件追加了内容;
        c、新文件的重名问题;
    4、对于大文件,可能复制的时间是比较长的,这可能带来一些新的边界条件;希望有用。
      

  2.   

    为什么要自己写?我觉得commons类库里一定有高效的算法的
      

  3.   


    import java.io.*;
    public class Copy {
      public static void main(String args[]) {
         FileInputStream fis = null;
         FileOutputStream fs = null;
         int b;
         try {
            fis = new FileInputStream("f:\\Classic\\ProducerConsumer.java");
            fs = new FileOutputStream("f:\\Classic\\Copy.txt");
          }catch( FileNotFoundException ie) {
          System.out.println(ie);
           }
         try {
            while((b=fis.read())!=-1) {
                fs.write(b);
             }
          }catch(IOException e) {
            System.out.println(e);
             }
       }
    }