package tyest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FileOper {
/**
* 读取并修改文件
*
* @param filepath 文件路径
* @param reg 需替换内容
* @param conter 替换后内容
* @return
*/
public String modifyFile(File file,String reg,String conter){
FileInputStream f=null;
InputStreamReader isr=null;
BufferedReader bf = null;
StringBuffer newLineString = new StringBuffer();
try {
f = new FileInputStream(file);
isr = new InputStreamReader(f,"utf-8");
bf = new BufferedReader(isr);
String lineString="";
while((lineString=bf.readLine()) != null){
if(lineString.contains(reg)){
newLineString.append(lineString.replace(reg, conter));
//newLineString=lineString.replace(text1, "../uilib");
}else{
newLineString.append(lineString);
}
newLineString.append("\r\n");
}
System.out.println(newLineString.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return newLineString.toString();
}
/**
* 保存文件
* @param filepath
* @param content
*/
public void saveFile(File file,String content){
FileOutputStream fos=null;
OutputStreamWriter ow=null;
try {
fos = new FileOutputStream(file);
ow= new OutputStreamWriter(fos,"utf-8");
try {
ow.write(content);
ow.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ow.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException{
FileOper fileOper = new FileOper();
File dir = new File("D:\\WorkShop\\solPage1\\page\\");
if(!dir.isDirectory()){
System.out.println("不是目录!");
return;
}
File[] f_ary=dir.listFiles();
if(f_ary.length>0){
for(File f:f_ary){
String filename = f.getName();
String filetype=filename.split("\\.")[1];
if("html".equals(filetype)){
String content=fileOper.modifyFile(f, "http://ssosol.sinosure.com.cn/esellc", "../uilib");
fileOper.saveFile(f, content);
}else{
continue;
}
}
}else{
System.out.println("没有文件");
}
}
}