/*
* 创建日期 2005-1-4
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.pub.file;
/**
* @author explorerwen
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public interface FileFunction {
public boolean makeDir(String filePath);
public boolean makeFile(String filePath,String content);
public boolean dltFile(String filePath);
public boolean copyFile(String surPath,String aimPath);
public boolean copyDir(String surPath,String aimPath);
public String rdLnFile(String filePath);
public boolean downFile(String urlPath,String aimPath);
public boolean webSaveToFile(String urlPath,String aimPath);
}
/*
* 创建日期 2004-12-31
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.pub.file;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
/**
* @author explorerwen
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class OprFileFun implements FileFunction{
private static OprFileFun oprFile = null;
public synchronized static OprFileFun getOprFile(){
if(oprFile == null)
oprFile = new OprFileFun();
return oprFile;
}
/**
* 创建文件夹
* filePath文件路径
* return true or false
*/
public boolean makeDir(String filePath){
try{
filePath = filePath.toString();
File myFilePath = new File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 创建文件
* filePath文件路径,content文件内容
* return true or false
*/
public boolean makeFile(String filePath,String content){
try{
filePath = filePath.toString();
content = content.toString();
File myFilePath = new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
myFile.println(content);
resultFile.close();
return true;
}catch(IOException ie){
ie.printStackTrace();
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 删除文件
* filePath文件路径
* return true or false
*/
public boolean dltFile(String filePath){
try{
filePath = filePath.toString();
File myDltFile = new File(filePath);
if(myDltFile.exists())
myDltFile.delete();
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 拷贝文件
* surPath 源文件路径;aimPath 目标文件路径
* return true or false
*/
public boolean copyFile(String surPath,String aimPath){
int byteread = 0;
byte [] buffer = new byte[1444];
try{
InputStream inStream = new FileInputStream(surPath.toString());
FileOutputStream fs = new FileOutputStream(aimPath.toString());
while((byteread = inStream.read(buffer))!=-1){
fs.write(buffer,0,byteread);
}
inStream.close();
return true;
}catch(FileNotFoundException ex){
ex.printStackTrace();
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 拷贝文件夹
* surPath 源文件路径;aimPath 目标文件路径
* return true or false
*/
public boolean copyDir(String surPath,String aimPath){
int byteread =0;
try{
surPath = surPath.toString();
aimPath = aimPath.toString();
if(makeDir(aimPath)){
File [] file = (new File(surPath)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i].toString());
FileOutputStream output=new FileOutputStream(aimPath+"/"+(file[i].getName()).toString());
byte [] buffer = new byte[1024*5];
while((byteread = input.read(buffer))!=-1){
output.write(buffer,0,byteread);
}
output.flush();
output.close();
input.close();
}
}
return true;
}else{
return false;
}
}catch(FileNotFoundException fn){
fn.printStackTrace();
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 按行读取文件
* filePath 文件路径
* return String 文件内容
*/
public String rdLnFile(String filePath){
String myStr = null;
String resultStr = "";
try{
FileReader myFileReader = new FileReader(filePath.toString());
BufferedReader myBufReader = new BufferedReader(myFileReader);
while((myStr = myBufReader.readLine())!=null){
resultStr +=myStr+"<br>";
}
myFileReader.close();
return resultStr;
}catch(FileNotFoundException ex){
ex.printStackTrace();
return "";
}catch(IOException ex){
ex.printStackTrace();
return "";
}catch(Exception ex){
ex.printStackTrace();
return "";
}
}
/**
* 直接从网上下载文件
* urlPath url,aimPath 保存路径
* return true or false
*/
public boolean downFile(String urlPath,String aimPath){
urlPath = urlPath.toString();
aimPath = aimPath.toString();
byte [] buffer = new byte[1444];
int byteread = 0;
try{
URL url = new URL(urlPath);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(aimPath);
while((byteread=inStream.read(buffer))!=-1){
fs.write(buffer,0,byteread);
}
return true;
}catch(MalformedURLException ex){
ex.printStackTrace();
return false;
}catch(IOException ex){
ex.printStackTrace();
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
/**
* 把网页保存成文件
* @param urlPath url
* @param aimPath 保存路径
* @return true or false
*/
public boolean webSaveToFile(String urlPath,String aimPath){
urlPath = urlPath.toString();
aimPath = aimPath.toString();
URL stdURL = null;
BufferedReader stdIn = null;
PrintWriter stdOut = null;
String strHtml = "";
try{
stdURL = new URL(urlPath);
stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
stdOut = new PrintWriter(new BufferedWriter(new FileWriter(aimPath)));
while((strHtml = stdIn.readLine())!=null){
stdOut.println(strHtml);
}
return true;
}catch(MalformedURLException ex){
ex.printStackTrace();
return false;
}catch(IOException ex){
ex.printStackTrace();
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}finally{
try{
if(stdIn != null)
stdIn.close();
if(stdOut != null)
stdOut.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String []args)throws Exception{
String url="http://www.sohu.com";
String filePath ="F:/test/sohu.html";
String filePath2 ="F:/test/test.html";
OprFileFun opr = OprFileFun.getOprFile();
boolean flag = opr.webSaveToFile(url,filePath);
boolean flag2 = opr.downFile(url,filePath2);
System.out.print(flag);
}
}