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");
}
}
* 创建日期 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");
}
}
相关文章推荐
- java遍历删除指定文件夹下面的相同格式或指定格式的file文件
- java遍历删除windows下的指定文件夹下面的相同格式或指定格式的file文件
- java基础之File对象中的FilenameFilter文件过滤以及递归列出指定文件夹路径下的所有文件和所有文件夹
- JAVA之IO技术-获取指定目录下的文件夹和文件的File对象或是字符串名称。
- JAVA之IO技术-获取指定目录下的文件夹和文件的File对象或是字符串名称。
- 使用java.nio.file.WatchService监控文件夹里的文件再操作文件出现的“文件不存在或目录不存在”的错误
- nested exception is java.io.FileNotFoundException: config.xml (系统找不到指定的文件。)
- Java关于遍历出指定文件夹里的文件列表
- Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
- java调用copy复制子文件夹及文件到指定目录(非xcopy)
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 用JAVA扫描指定文件夹下面所有以.txt结尾的文件,并将其绝对路径输出
- File文件基本操作之二:java 替换指定文件中的指定内容
- Java实现将指定文件夹内的指定类型的文件归类
- java 循环文件夹删除指定名称的文件
- Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)
- Java I/O之FilenameFilter类列举出指定路径下某个扩展名的文件
- java文件过滤器(File类listFiles方法可选参数)--获取目录下指定的某类文件
- Java读取文件夹下的指定类型的文件 递归筛选 包含子文件夹
- java变量指定目录下所以的文件和文件夹