| 发表于:2007-03-17 00:14:114楼 得分:0 |
哦,是的,拷贝的可以是目录,这里有个很好的程序,供参考,有次看到的, controlfile.java 拷贝莫路径下的所有文件 import java.io.*; import java.text.*; import java.util.*; /** * @author ljt * * to change this generated comment edit the template variable "typecomment ": * window> preferences> java> templates. * to enable and disable the creation of type comments go to * window> preferences> java> code generation. */ public class controlfile { //已有文件的路径,需要备份到那个路径的名字 string filepath, aimfilepath; //保存已有文件路径下的所有的文件的名字 存放string vector vec; public controlfile() { filepath = " "; aimfilepath = " "; vec = new vector(); } public controlfile(string filepath, string aimfilepath) { this.filepath = filepath; this.aimfilepath = aimfilepath; vec = new vector(); } //得到目录下所有文件的名字 private void getfilename() { file f = new file(filepath); string str[] = f.list(); for (int i = 0; i < str.length; i++) { vec.addelement(str[i]); } } // 文件的拷贝:::测试成功 private boolean bakfile(string filename) { try { //读文件 filereader raf = new filereader(filepath + filename); string detail = " "; bufferedreader buff = new bufferedreader(raf); string temp = buff.readline(); while (temp != null) { detail += temp + "\n "; temp = buff.readline(); } raf.close(); system.out.println(detail); //写文件 file file = new file(aimfilepath + filename); printwriter out = new printwriter(new filewriter(file)); out.print(detail); out.close(); } catch (filenotfoundexception e) { system.out.println( "文件没有找到 "); } catch (ioexception e) { system.out.println( "copyfile 出错 "); } return true; } public static void main(string[] args) { controlfile confile = new controlfile( "d:\\readfile\\ ", "d:\\work\\bakfile\\ "); confile.getfilename(); vector ve = new vector(); ve = confile.vec; if (ve != null) for (int i = 0; i < ve.size(); i++) { system.out.println((string) ve.elementat(i)); confile.bakfile((string) ve.elementat(i)); } } } | | |
|