Java文件操作里面的文件复制问题

有一个java FCopy 源文件名 新文件名
的语句,,相关操作语句是怎样可以实现文件复制操作,用已有文件复制出新的文件
或者介绍一种别的文件复制操作方法
例题代码如下,,,我不知道怎样该写才能让他运行起来

FCopy 源文件名 新文件名
package package9;
import java.io.*;
public class T905FCopy {

public static void main(String[] args) {
if(args.length!=2){
System.out.println("参数命令不对,正确格式为:FCopy 源文件名 新文件名");
System.exit(0);
}
try{
FileInputStream in=new FileInputStream(args[0]);
FileOutputStream out=new FileOutputStream(args[1]);
while(in.available()>0){
int i=in.read();out.write(i);
}
in.close();out.close();
System.out.println("文件复制成功");
}
catch(Exception e){System.out.println("文件复制失败");}
}
}

Java编程文件操作,将一个文件的内容复制到另一个文件中,案例代码如下:

package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
/**
 * å°†ä¸€ä¸ªæ–‡ä»¶çš„内容复制到另一个文件中 è¦é‡‡è¾¹è¯»è¾¹å†™çš„模式,这样效率才会高
 * 
 * @author Administrator
 *
 */
public class Copy {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(args.length);
        /**
         * åœ¨args参数中传进两个文件的路径,可以在run as->run configuration的arguments设置args的参数
         *
         */
        if (args.length != 2) {
            System.out.println("输入的参数不正确!");
            System.exit(1);
        }
        File file1 = new File(args[0]);
        File file2 = new File(args[1]);
 
        if (!file1.exists()) {
            System.out.println("源文件不存在!");
 
        }
        InputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file1);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        OutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file2, true);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        if (fileInputStream != null && fileOutputStream != null) {
            int temp = 0;
            try {
                /**
                 * è¾¹è¯»è¾¹å†™
                 */
                while ((temp = fileInputStream.read()) != -1) {
                    fileOutputStream.write(temp);
                }
                System.out.println("复制完成");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("复制失败");
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
 
        }
 
    }
 
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-17
java复制文件不是用io流读取输出吗?我向来用这个追问

有完整代码可以贴一下吗,谢谢

追答

public class Copy {
public static void main(String[] args) {
File oldfile = new File("E:/原文件.txt");
File newfile = new File("E:/复制文件.txt");
try {
FileInputStream fis = new FileInputStream(oldfile);
FileOutputStream fos = new FileOutputStream(newfile);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int a;
while((a=bis.read())!=-1){
bos.write(a);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

本回答被提问者采纳
第2个回答  2013-10-18
如果没有ide的话,就在dos或者终端下,运行javac ./.../T905FCopy.java 如果成功生成.class文件运行 java T905FCopy
第3个回答  2013-10-17
我只记得边用inputStream读,边用outPutStream写。具体的代码我先写写看,也想知道你说的这种简单方法
FileInputStream in=new FileInputStream("f:/test.txt");
FileOutputStream out=new FileOutputStream("d:/test.txt");
byte[] bytes=new byte[512];
int len=0;
while((len=in.read(bytes))!=-1){
out.write(bytes, 0, len);
}
然后把流关闭,再抛异常就可以了
还是你自己上面的代码简单
不过查了下建议你不要那样写,那样应该是一个字节一个字节的边读取边写入,效率会比较低
相似回答