一个简单的文件拷贝程序,求教我错在哪了?
代码如下:
import java.io.*;
import java.util.*;
public class FileCopy
{
public FileCopy(String s,String a)
{
File fs=new File(s);
File fa=new File(a);
/**
读入并拷贝数据
*/
try
{
BufferedInputStream fsIn = new BufferedInputStream(new FileInputStream(fs));
BufferedOutputStream faOut = new BufferedOutputStream(new FileOutputStream(fa));
//以下6行为有问题的代码,我不知道错在哪了?
int i=fsIn.read();
while(i!=-1)
{
faOut.write(i);
i=fsIn.read();
}
fsIn.close();
faOut.close();
}catch(
IOException e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
System.out.println( "Start copy..... ");
long l1=System.currentTimeMillis();
new FileCopy(args[0],args[1]);
long l2=System.currentTimeMillis();
System.out.println( "Finished! ");
System.out.println( "Total Time: "+(l2-l1));
}
}
args[0]为欲拷贝的文件,args[1]为拷贝文件。谢谢!
------解决方案--------------------这个,代码虽然写的不太好,但是不至于出错,我估计你是输入的参数错了.
改成new FileCopy( "c:/a.txt ", "c:/aaaa.txt ");试试.注意分隔符 "/ "或者 "\\ "
------解决方案--------------------给你个代码吧,很简单.
这个用字符流复制文件
import java.io.*;
public class WriteFile{
public static void main(String[] args){
try{
BufferedReader in=new BufferedReader(new FileReader(args[0]));
PrintWriter out=new PrintWriter(args[1]);
String s=in.readLine();
while(s!=null)
{
out.println(s);
s=in.readLine();
}
in.close();
out.close();
}catch(IOException e){
System.out.println(e);
}
}
}
下面用字节流复制文件
import java.io.*;
public class CopyFile{
/*public static void copyFile(DataInputStream in,DataOutputStream out) throws IOException{
try{
while(true)
{
out.writeByte(in.readByte());
}
}catch(
EOFException eof){
return;
}
}*/
public static void main(String[] args){
try{
DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(args[0])));
DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(args[1])));
//copyFile(in,out);
while(in.available()!=0)
{
out.writeByte(in.readByte());
}
in.close();
out.close();
}catch(IOException e){
System.out.print(e);
}
}
}
------解决方案--------------------看着不像是有错的样子