package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
public class DoFile {
public boolean PrintPage(String comePage,String comeFile,String toPage,String toFile) throws IOException {
boolean flag=false;
FileInputStream fis=null;
FileOutputStream fos=null;
System.out.println("源文件夹 地址:"+comePage);
System.out.println("您要复制的文件是: "+comeFile);
System.out.println("目标文件夹地址: "+toPage);
System.out.println("新生成的文件:"+toFile);
try {
//判断源文件夹是否存在,是否yi /结尾,如果没有,要进行添加
if(comePage.endsWith("/")){//继续执行,不必改变
System.out.println("是以 / 结尾的");
}
else{
System.out.println("您输入的源文件夹最后应以/ 结尾,不过没关系,系统帮您加上了");
StringBuffer newComePage=new StringBuffer(comePage);
newComePage.append("/");
System.out.println("newComePage: "+newComePage);
comePage=newComePage.toString();
}
//判断文件是否存在,不存在,就反馈给用户
File ff=new File(comePage+comeFile);
if(ff.exists()){
System.out.println("存在这个文件");
}
else{
System.out.println("您指定的文件不存在");
return false;
}
//判断源文件夹是否存在,是否yi /结尾,如果没有,要进行添加
System.out.println("目标文件夹 地址:"+toPage);
if(toPage.endsWith("/")){//继续执行,不必改变
System.out.println("是以 / 结尾的");
}
else{
System.out.println("您输入的源文件夹最后应以/ 结尾,不过没关系,系统帮您加上了");
StringBuffer newComePage=new StringBuffer(toPage);
newComePage.append("/");
System.out.println("newComePage: "+newComePage);
toPage=newComePage.toString();
}
//判断目标文件夹是否存在,不存在就创建
File dd=new File(toPage);
if(dd.exists()){
System.out.println("目标文件夹存在,不必创建");
}
else{
System.out.println("目标文件夹不存在,不过,别担心,系统帮您创建了");
dd.mkdirs();
}
//进行文件拷贝
fis=new FileInputStream(comePage+comeFile);
fos=new FileOutputStream(toPage+toFile);
//中转数组
byte[] buff=new byte[1024];
int readed=-1;
while((readed=fis.read(buff))>0)
fos.write(buff, 0, readed);
flag=true;
} catch (FileNotFoundException e) {
System.out.println("请确切源文件夹中存在您指定的文件");
e.printStackTrace();
flag=false;
}
finally{
fis.close();
fos.close();
return flag;
}
}
public static void main(String[] args) throws IOException {
System.out.println("begin");
DoFile df=new DoFile();
df.PrintPage("f:/source","old.txt","f:/now/","new.txt");
System.out.println("success");
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
public class DoFile {
}