疯狂Java讲义习题11.3
2017-08-06 18:16
218 查看
题目描述:
实现一个工具类,该工具可实现copy功能,若被copy的对象是文件,程序将制定文件复制到制定目录下,如过被copy对象是目录,程序应将该目录及其目录下的所有文件复制到指令目录下。
代码如下:
点击(此处)折叠或打开
import java.io.*;
public class cpft
{
public static void copyFile(String dst,String src) throws IOException
{
FileOutputStream fos = null;
FileInputStream fis = null;
fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
fis = new FileInputStream(src);
int hasRead = 0;
byte[] bbuf = new byte[1024];
while ((hasRead = fis.read(bbuf)) > 0)
{
fos.write(bbuf,0,hasRead);
}
}
public static void copy(String dst,String src) throws IOException
{
File newFile = new File(src);
File[] fileList = newFile.listFiles();
//File dstFile = new File(dst+"//"+newFile.getName());
if (newFile.isFile())
{
copyFile(dst,src);
}
else
{
File dstFile = new File(dst+"//"+newFile.getName());
if (!dstFile.exists())
{
dstFile.mkdirs();
}
for (File file:fileList)
{
if (file.isFile())
copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());
else
copy(dstFile.getAbsolutePath(),file.getAbsolutePath());
}
}
}
public static void main(String[] args) throws IOException
{
String src = "F:\\paper";
String dst = "F:\\冰点文库";
copy(dst,src);
System.out.println("finished Copy");
}
}
运行结果:(完成复制)
finished Copy
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(65) | 评论(0) | 转发(0) |
0
上一篇:疯狂Java讲义习题11.2
下一篇:疯狂java讲义习题11.4
相关热门文章
Tomcat 6 配置SSI
让Resin支持shtml(SSI)- 静...
tomcat + ssi
ASP JavaScript Lessons(8-14)
JDK1.6官方下载_JDK6官方下载_...
给主人留下些什么吧!~~
评论热议
实现一个工具类,该工具可实现copy功能,若被copy的对象是文件,程序将制定文件复制到制定目录下,如过被copy对象是目录,程序应将该目录及其目录下的所有文件复制到指令目录下。
代码如下:
点击(此处)折叠或打开
import java.io.*;
public class cpft
{
public static void copyFile(String dst,String src) throws IOException
{
FileOutputStream fos = null;
FileInputStream fis = null;
fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
fis = new FileInputStream(src);
int hasRead = 0;
byte[] bbuf = new byte[1024];
while ((hasRead = fis.read(bbuf)) > 0)
{
fos.write(bbuf,0,hasRead);
}
}
public static void copy(String dst,String src) throws IOException
{
File newFile = new File(src);
File[] fileList = newFile.listFiles();
//File dstFile = new File(dst+"//"+newFile.getName());
if (newFile.isFile())
{
copyFile(dst,src);
}
else
{
File dstFile = new File(dst+"//"+newFile.getName());
if (!dstFile.exists())
{
dstFile.mkdirs();
}
for (File file:fileList)
{
if (file.isFile())
copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());
else
copy(dstFile.getAbsolutePath(),file.getAbsolutePath());
}
}
}
public static void main(String[] args) throws IOException
{
String src = "F:\\paper";
String dst = "F:\\冰点文库";
copy(dst,src);
System.out.println("finished Copy");
}
}
运行结果:(完成复制)
finished Copy
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(65) | 评论(0) | 转发(0) |
0
上一篇:疯狂Java讲义习题11.2
下一篇:疯狂java讲义习题11.4
相关热门文章
Tomcat 6 配置SSI
让Resin支持shtml(SSI)- 静...
tomcat + ssi
ASP JavaScript Lessons(8-14)
JDK1.6官方下载_JDK6官方下载_...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- 疯狂java讲义习题11.1
- 疯狂java讲义习题11.4
- 疯狂java讲义习题11.5
- 疯狂Java讲义中的习题,设计一个控制台的梭哈游戏
- 疯狂java讲义习题7.2
- 疯狂java讲义习题7.3
- 疯狂java讲义中的习题,设计一个简单的计算器。
- 疯狂Java讲义432页习题CopyPerson出错(未解决)
- 疯狂java讲义--第三章习题 画圆
- 疯狂Java讲义中的习题,设计一个控制台的五指棋游戏
- 《疯狂 Java 讲义》包含PDF电子书,源码,课后习题解析
- 疯狂java讲义之多线程
- 疯狂JAVA讲义---第七章(中):集合List,Queue(转)
- java疯狂讲义第三版
- 疯狂java讲义 第三章 数据类型和运算符
- JAVA疯狂讲义第三版4.6.6数组的应用举例代码功能扩展
- Java疯狂讲义读书笔记第十章
- 疯狂java讲义之泛型
- 疯狂JAVA讲义---第十二章:Swing编程(一)基本组件
- 疯狂JAVA讲义---第十二章:Swing编程(七)JTree树