?
//
任務1:
// 寫一個文件拷貝函數:?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]);
????????????}
????????}
????}
????
}
// 寫一個文件拷貝函數:?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]);
????????????}
????????}
????}
????
}