求助!!!
刚学JavaSE,有许多的不懂,为何在代码的第85行的Try语句里面把srcFile改为this.srcFile就会导致程序不运行这一块的内容呢?我有在里面增加了syso(“1”)的操作,是无法输出1的。
有大神能帮忙解答一下吗?谢谢!
try {
input = new FileInputStream(srcFile);
input = new FileInputStream(this.srcFile);
output = new FileOutputStream(desFile);
input.transferTo(output);
return true;
}
/*
实现文件拷贝处理
需求分析:
1、文件的拷贝操作,可能拷贝各种文件,所以肯定使用字节流;
2、可能存在大文件;
方案1、使用InputStream将全部要拷贝的文件内容直接读取到程序里面,而后一次性输出到目标文件;
缺点:如果拷贝的文件很大,程序基本上就死了;
方案2、采用部分拷贝,读取一部分输出一部分数据。
核心:
InputStream:public int read(byte[] b)throws IOException
OutputStream:public void write (byte[] b, int off,int len)throws IOException
但是需要注意的是,以上的做法是属于文件拷贝的最原始实现,而从JDK1.9开始InputStream和Reader类中都追加有数据
转存的处理操作方法:
1、InputStream:public long transferTo(OutputStream out)throws IOException;
2、Reader:public long transferTo(Writer out)throws IOException;
*/
package 第16章字节流与字符流;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class JavaDemo9 {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
System.out.println("命令执行错误,执行结构:java JavaDemo8 拷贝源文件路径 拷贝目标文件路径");
System.exit(1);
}
long start = System.currentTimeMillis();
FileUtil1 fu = new FileUtil1(args[0],args[1]);
if(new File(args[0]).isFile()) { //文件拷贝
System.out.println(fu.copy() ?"文件拷贝成功!":"文件拷贝失败!");
}else { //目录拷贝
System.out.println(fu.copyDir() ?"目录拷贝成功!":"目录拷贝失败!");
}
long end = System.currentTimeMillis();
System.out.println("拷贝完成所花费的时间:" + (end - start) + "ms");
}
}
class FileUtil1{ //定义一个文件操作的工具类
private File srcFile; //源文件路径
private File desFile; //目标文件路径
public FileUtil1(String src,String des) {
this(new File(src),new File(des));
}
public FileUtil1(File srcFile,File desFile) {
this.srcFile = srcFile;
this.desFile = desFile;
}
public boolean copyDir() throws Exception{
try {
this.copyImpl(this.srcFile);
return true;
} catch (Exception e) {
return false;
}finally {
}
}
private void copyImpl(File file) throws Exception{ //递归操作
if(file.isDirectory()) { //是目录
File results[] = file.listFiles(); //列出全部目录组成
if(results != null) {
for( int x = 0;x<results.length;x++) {
copyImpl(results[x]);
}
}
}else { //是文件
String newFilePath = file.getPath().replace(this.srcFile.getPath() + File.separator,"");
File newFile = new File(this.desFile,newFilePath); //拷贝的目标路径
this.copyFileImpl(file, newFile);
}
}
private boolean copyFileImpl(File srcFile,File desFile) throws Exception{
if(!desFile.getParentFile().exists()) {
desFile.getParentFile().mkdirs(); //创建目标路径的父目录
}
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(srcFile);
output = new FileOutputStream(desFile);
input.transferTo(output);
return true;
} catch (Exception e) {
throw e;
}finally {
if(input != null) {
input.close();
}
if(output != null) {
output.close();
}
}
}
public boolean copy() throws Exception{ //文件拷贝处理
if(!this.srcFile.getParentFile().exists()) { //源文件必须存在
System.out.println("拷贝的源文件不存在!");
return false; //拷贝失败
}
return this.copyFileImpl(this.srcFile, this.desFile);
}
}
第