需求描述:将某一个路径下的所有html文件中的“客户名称”更改为新的文件名。
项目结构
test.html
只截取了其中一小段,屏蔽了一些信息,我们要取的名字是“李四四”
<script language="JavaScript" src="viewctrl.js"></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>个人信用报告</TITLE>
<STYLE type=text/css></STYLE>
<META content="Microsoft FrontPage 5.0" name=GENERATOR>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/style1.css" type="text/css">
<style type="text/css">
.tdStyle {
border: 1px solid;
}
</style>
<style type="text/css">
<!--
.style1 {
color: #0066cc;
font-weight: bold;
}
.style4 {
color: #0066cc;
font-size: 14px;
}
TD {
FONT-FAMILY: 宋体;
FONT-SIZE: 9pt
}
-->
</style>
<script language="JavaScript">
function msover(){event.srcElement.style.backgroundColor="#ccffff";event.srcElement.style.cursor = "hand";}function msout(){event.srcElement.style.backgroundColor="#FFF6DC";event.srcElement.style.cursor = "auto";}
</script>
</HEAD>
<BODY>
<div align="center">
<center>
<p></p>
</center>
</div>
<table width="720" cellspacing="0" cellpadding="0" align="center"
style="border-collapse: collapse; border: none">
<tr>
<td height="117" align="center">
<table height=95 cellspacing=0 cellpadding=0 width=720
align="center" style="border-collapse: collapse; border: none">
<tbody>
<tr align="center" valign="middle">
<td height=24 colspan=3>
<div align=center>
<font color=#0066cc size=6><b><font face="黑体">个人信用报告</font></b></font>
</div>
</td>
</tr>
<tr align="center" valign="middle">
<td height=24 colspan=3>
<div align=center>
<font color=#0066cc size=3><b><font face="黑体">
</font></b></font>
</div> <br>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<table width="620" align="center" cellspacing=0 cellpadding=0
style="border-collapse: collapse; border: none">
<tbody>
<tr>
<td>
<table cellspacing=0 cellpadding=0 width="620" border="1"
align="center"
style="border-collapse: collapse; border: none; border-color: black">
<tr height=30 align="center" valign="middle">
<td width="20%" class="tdStyle"><font color=#0066cc>被查询者姓名</font>
</td>
<td class="tdStyle" width="20%"><font color=#0066cc>被查询者证件类型</font>
</td>
<td class="tdStyle" width="20%"><font color=#0066cc>
被查询者证件号码</font></td>
<td class="tdStyle" width="20%"><font color=#0066cc>查询操作员</font>
</td>
<td class="tdStyle" width="20%"><font color=#0066cc>查询原因</font>
</td>
</tr>
<tr height=30>
<td class="tdStyle" width="20%">
<div class=high align=center>
<font color=#0066cc><span class=high>李四四</span></font>
</div>
</td>
<td class="tdStyle" width="20%">
<div class=high align=center>
<font color=#0066cc><span class=high>身份证</span></font>
</div>
</td>
<td class="tdStyle" width="20%">
<div class=high align=center>
<font color=#0066cc><font color=#0066cc>123456789987654321</font></font>
</div>
</td>
</tr>
</table>
</td>
</tr>
<style type="text/css">
</style>
</table>
</BODY>
editRhFileName.java
package com.hs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
*
* 修改文件名
*
* @version [版本号, 2016年11月28日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class editRhFileName
{
/**
*
* 测试main方法
*
* @param args
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
// 自动生成方法存根
String path = "d:/old/";
editHtmlToCustName(path);
}
/**
*
* 修改文件名称为客户名称
*
* @param filePath
* @see [类、类#方法、类#成员]
*/
public static boolean editHtmlToCustName(String filePath)
{
// 是否修改成功
boolean flag = false;
// 自动生成方法存根
File f = new File(filePath);
// 新的文件名
String newFileName = "";
// 获取该目录的所有文件
File[] tempList = f.listFiles();
// 循环
for (int i = 0; i < tempList.length; i++)
{
//是文件修改文件名称
if (tempList[i].isFile())
{
System.out.println("文 件:" + tempList[i]);
// 解析方法
newFileName = getHtmlCustName(tempList[i]);
File newFile = new File(getNewFilePath(filePath, newFileName));
if (tempList[i].renameTo(newFile))
{
System.out.println("修改成功!");
flag = true;
}
else
{
System.out.println("修改失败");
flag = false;
}
}
//是文件夹则递归继续修改文件名
else if (tempList[i].isDirectory())
{
System.out.println("文件夹:" + tempList[i]);
System.out.println(tempList[i].getPath());
//递归
editHtmlToCustName(tempList[i].getPath());
}
else
{
System.out.println("发生未知错误");
}
}
return flag;
}
/**
*
* 从文件中加载 HTML文档,得到客户名称
*
* @param input
* @return
* @see [类、类#方法、类#成员]
*/
public static String getHtmlCustName(File input)
{
String custName = "";
try
{
Document doc = Jsoup.parse(input, "UTF-8", "http://www.oschina.net/");
//获取class=high中的第一个元素
Element nameElement = doc.select("span[class=high]").first();
//获取元素值
custName = nameElement.text();
System.out.println(custName);
}
catch (IOException e)
{
e.printStackTrace();
}
return custName;
}
/**
*
* 获取新文件名称路径
*
* @param filePath
* @param newFileName
* @return
* @see [类、类#方法、类#成员]
*/
public static String getNewFilePath(String filePath, String newFileName)
{
return filePath + "/" + newFileName + ".html";
}
/**
*
* 文件转为字符串(此处未用)
*
* @param src
* @return
* @see [类、类#方法、类#成员]
*/
@SuppressWarnings("resource")
public String readFromFile(File src)
{
try
{
BufferedReader bufferedReader = new BufferedReader(new FileReader(src));
StringBuilder stringBuilder = new StringBuilder();
String content;
while ((content = bufferedReader.readLine()) != null)
{
stringBuilder.append(content);
}
return stringBuilder.toString();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
在D:\old目录下放入test.html文件的副本,并新建一个文件夹,将这个文件拷贝到新建的文件夹下
右键editRhFileName->run as->java application
文件夹中以及子文件夹中的文件名都被修改成功