对java.io.*;熟的朋友.进来帮帮助

A.txt和B文件夹,这两个文件都在E:盘下
如何把一个A.txt文件复制到一个B文件夹下。。
请大家帮帮助。
[67 byte] By [dazhen520-大真] at [2008-1-9]
# 1
这个题目和我在时力科技的时候的面试题差不多啊。
原题是这样的:编写一个CopyFile 实现在dos窗口中从键盘输入两个文件名完成文件的复制功能
java CopyFile file1 file2?
guaxixi627 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 2
public boolean copyFile(String from,String to){

File fromFile,toFile;

fromFile = new File(from);

toFile = new File(to);

FileInputStream fis = null;

FileOutputStream fos = null;

try{

toFile.createNewFile();

fis = new FileInputStream(fromFile);

fos = new FileOutputStream(toFile);

int bytesRead;

byte[] buf = new byte[4 * 1024];// 4K buffer

while((bytesRead=fis.read(buf))!=-1){

fos.write(buf,0,bytesRead);

}

fos.flush();

fos.close();

fis.close();

}catch(IOException e){

System.out.println(e);

return false;

}

return true;

}
lixiaoxue85-蛮野蛮 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 3
import java.io.*;

public class CopyFile
{
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(
new FileReader("E:\\A.txt"));
PrintWriter pw=new PrintWriter(
new FileWriter("E:\\B\\A.txt"));
String s;
while((s=br.readLine())!=null)
pw.println(s);
pw.close();
}
}
biaoflying-biaoflying at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 4
如果B目录可能不存在的话,最好用File里的exists()判一下是不是存在这个目录
A.txt文件的话,也是一个道理
# 5
4楼的兄弟,如果碰到一个文件大于4k该如何处理呢
以前记得再那里见过一段代码,如果超过的话
就再分几次进行处理,具体我也忘了,呵呵
haoyipeng-呵呵虫 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 6
biaoflying() 回答得很好了...
victorypig-java猪 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 7
biaoflying()回答的代码非常好啊
yanj_20 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 8
谢谢,各位好友。。。我懂了。。。
dazhen520-大真 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 9
File f1 = new File("xxx"),f2 = new File("xxx");
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
int i = 0 , byte[] by = new byte[1024];
try
{
while((i = fis.read(by))!=-1)
{
if(i!=0)fos.write(by,0,i);
}
}catch(IOException e)
{
e.printStackTrace();
}finally
{
try
{
fis.close();
fos.close();
}catch(IOException e){ e.printStackTrace();}
}
# 10
import java.io.*;
public class Test
{
public boolean copyfile(String src,String dec)
{
File srcFile=new File(src);
File decFile=new File(dec);

try
{

FileInputStream fis=new FileInputStream(srcFile);
FileOutputStream ops=new FileOutputStream(decFile);
int n;
byte buff[]=new byte[1024*4];
decFile.createNewFile();

while((n=fis.read(buff))!=-1)
{
ops.write(buff,0,n);

}
ops.flush();
fis.close();
ops.close();

}

catch(FileNotFoundException e)
{

System.out.println(e);
}
catch(IOException er)
{

System.out.println(er);
return false;
}
return true;

}

public static void main(String[] args)
{
Num6 cf=new Num6();
String src = "c:/a.txt";
String des = "c:/B/c.txt";
if(cf.copyfile(src,des))
{

System.out.println("拷贝成功");

}
else
{
System.out.println("拷贝失败");
}
}

}
fanxiangqin-mary at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...
# 11
用File类的renameTo(new File("E:/A.txt"));放心,这是移动,不是复制.
hoverlees-好棒 at 2007-10-19 > top of Msdn China Tech,Java,J2SE,基础类...