java怎么实现文件拷贝功能
4个回答
2012-05-10
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public boolean copy(String file1,String file2) {
File in=new File(file1);
File out=new File(file2);
if(!in.exists()){
System.out.println(in.getAbsolutePath()+"源文件路径错误!!!");
return false;
}
else {
System.out.println("源文件路径"+in.getAbsolutePath());
System.out.println("目标路径"+out.getAbsolutePath());
}
if(!out.exists())
out.mkdirs();
File[] file=in.listFiles();
FileInputStream fin=null;
FileOutputStream fout=null;
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
try {
fin=new FileInputStream(file[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("in.name="+file[i].getName());
try {
fout=new FileOutputStream(new File(file2+"/"+file[i].getName()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(file2);
int c;
byte[] b=new byte[1024*5];
try {
while((c=fin.read(b))!=-1){
fout.write(b, 0, c);
System.out.println("复制文件中!");
}
<------------------------------注意
fin.close();
fout.flush();
fout.close();
<--------------------------------
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<-------------------------------注释掉
// return true;
}
else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}
return false;
}
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("E:\\study\\opngl", "E:\\opengl");
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public boolean copy(String file1,String file2) {
File in=new File(file1);
File out=new File(file2);
if(!in.exists()){
System.out.println(in.getAbsolutePath()+"源文件路径错误!!!");
return false;
}
else {
System.out.println("源文件路径"+in.getAbsolutePath());
System.out.println("目标路径"+out.getAbsolutePath());
}
if(!out.exists())
out.mkdirs();
File[] file=in.listFiles();
FileInputStream fin=null;
FileOutputStream fout=null;
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
try {
fin=new FileInputStream(file[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("in.name="+file[i].getName());
try {
fout=new FileOutputStream(new File(file2+"/"+file[i].getName()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(file2);
int c;
byte[] b=new byte[1024*5];
try {
while((c=fin.read(b))!=-1){
fout.write(b, 0, c);
System.out.println("复制文件中!");
}
<------------------------------注意
fin.close();
fout.flush();
fout.close();
<--------------------------------
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<-------------------------------注释掉
// return true;
}
else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}
return false;
}
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("E:\\study\\opngl", "E:\\opengl");
}
}
更多追问追答
追问
如果目标文件夹和源文件都不确定该怎么写???
追答
那就麻烦了你必须加一个界面用来选择文件,同时界面上有输出路径可以修改。网上搜一下吧。java无直接复制文件的方法,必须先读在写。
参考资料: http://zhidao.baidu.com/question/376443812.html
展开全部
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制文件操作出错");
e.printStackTrace();
}
}
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制文件操作出错");
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用IO流的读取,和输出!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询