Posted on 2006-08-10 23:41
duyouyou.com 閱讀(1583)
評論(1) 編輯 收藏 所屬分類:
web技術
本人原創,轉載請注明出處:http://m.tkk7.com/web/
//
任務:
//
寫一個文件拷貝函數:?fileCopy(String?a?,String?b)???
//
a--表示原文件名???b--表示目標文件名擴展:
//
如果a是文件,則copy?a到b?;
//
如果a是目錄,則copy?a下的所有文件和文件夾(包括子文件夾)到b目錄下。
//
import
?java.io.
*
;
public
?
class
?IODemo?{
????
????
public
?
void
?fileCopy(String?a,?String?b){
????????File?file?
=
?
new
?File(a);
????????
if
(
!
file.exists()){
????????????System.out.println(a?
+
?
"
?Not?Exists.
"
);
????????????
return
;
????????}
????????File?fileb?
=
?
new
?File(b);
????????
if
(file.isFile()){
????????????FileInputStream?fis?
=
?
null
;
????????????FileOutputStream?fos?
=
null
;
????????????
try
?{
????????????????fis?
=
?
new
?FileInputStream(file);
????????????????fos?
=
??
new
?FileOutputStream(fileb);
????????????????
????????????????
byte
[]?bb?
=
new
?
byte
[?(
int
)file.length()];
????????????????fis.read(bb);
????????????????fos.write(bb);
????????????}
catch
?(IOException?e){
????????????????e.printStackTrace();
????????????}
finally
{
????????????????
try
?{
????????????????????fis.close();
????????????????????fos.close();
????????????????}?
catch
?(IOException?e)?{
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
else
?
if
(file.isDirectory()){
????????????
if
(
!
fileb.exists()){
????????????????fileb.mkdir();
????????????}
????????????String[]?fileList;
????????????fileList?
=
?file.list();
????????????
for
(
int
?i?
=
?
0
;?i?
<
?fileList.length;?i
++
){
????????????????fileCopy(a?
+
?
"
\\
"
?
+
?fileList[i],b?
+
?
"
\\
"
?
+
?fileList[i]);
????????????}
????????}
????}
????
}