您的位置:首页 > 编程语言 > Java开发

UpdateFile.java(更新指定文件夹的文件)作者:阿飞

2005-01-03 15:37 489 查看
/*
* 创建日期 2004-12-14
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package net.selfcafe;

import java.io.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class UpdateFile
{

//遍历整个文件加
public static void SearchAllDec(String path) {

int size;
String subpath="";

File newfile = new File(path);

if (newfile.isDirectory()) {

//System.out.println(newfile.getName());
String filelist[] = newfile.list();
size = filelist.length;
for (int i = 0; i < size; i++) {
subpath = path + "/" + filelist[i];
SearchAllDec(subpath);
}
} else if (newfile.isFile()) {

//得到文件路径
String strFilePath = newfile.getAbsolutePath();
String strFileContent = "";
String strFileName = newfile.getName();
String strFileExt = strFileName.substring( strFileName.length()-3,strFileName.length() );
//如果是asp文件,得到文件中的内容

strFileContent = getStrFromFile(strFilePath);

if (strFileExt.equals("asp")){

MakeNewFile(path,strFileName,strFileContent);
}

} else {
System.out.println("no such file or directory");
}

}

/*
*
* 得到文件的内容
* */
public static String getStrFromFile(String strFileNameAndPath)
{

BufferedReader br= null;

StringBuffer s=new StringBuffer();
String line = "";
int iLineCount = 0;
int iSum = 0;
try{
br = new BufferedReader(new FileReader(strFileNameAndPath));
iLineCount = 0;

while( (line=br.readLine())!=null)
{
//处理页面如果,如果包含框架的去掉
iLineCount ++;
String strResult; //临时字符串
//boolean iChangeFlag = false;//保证成对出现
strResult = "<iframe src=/"/ini/top.asp?pid=<%=pid%>/" frameborder=/"0/" scrolling=/"no/" width=/"778/" height=/"145/">";
if (line.indexOf(strResult)>-1)
{
System.out.println("Line:"+ iLineCount);
System.out.println(line);
iSum++;
line = "<td>/n";

}
strResult = "</iframe></td>";
if (line.indexOf(strResult)>-1)
{
System.out.println("Line:"+ iLineCount);
System.out.println(line);
line = "</td>/n";
}
s.append(line);
s.append("/n");
}
System.out.print(iSum+ "");

}catch(Exception e){

}

String result = new String(s);
result = result.trim();
return result;
}
/*
* ==生成新的页面
*
* */

public static boolean MakeNewFile(String strFilePath ,String strFileName,String newsContent){
File path=null;
File d=null;
//System.out.println(strNewsPath);
try
{
path=new File(strFilePath);
//System.out.println("1");
if(!path.exists())
path.mkdir();

System.out.println(" 文件名称:"+strFilePath);
//System.out.println(strFileName);
String tempStr = strFilePath.trim();

//写入html文件
FileWriter fw;
try
{
fw=new FileWriter(tempStr);//建立FileWriter对象,并实例化fw
//将字符串写入文件
fw.write(newsContent);
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}catch(Exception e){
System.err.println(e);
e.printStackTrace();
return false;
}finally{
path=null;
d=null;
}

return true;
}

public static void main(String[] args)
{
SearchAllDec("h:/test");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐