`
cywhoyi
  • 浏览: 418661 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ReplaceContent

    博客分类:
  • JAVA
F# 
阅读更多
public class ModifyContent {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException,
			UnsupportedEncodingException {
		List<String> list = readFileContent();
		List<String> chapterlist = getChapterNames();
		Map<String, String> chapterMap = new HashMap<String, String>();
		for (int i = 0; i < chapterlist.size(); i++) {
			chapterMap.put(chapterlist.get(i), list.get(i));
		}
		BookMusicBean.getInstance().setMap(chapterMap);
		Map<String, String> targetMap = BookMusicBean.getInstance().getMap();
		String to = "";
		String from = "";
		for (int i = 0; i < chapterlist.size(); i++) {
			from = chapterlist.get(i);
			to = targetMap.get(chapterlist.get(i));
			System.out.println("to=" + to + ",from=" + from);
			replace("d:/chen2010.txt", from, to);
		}
		// replace("d:/chen2010.txt", "###第2章 月蚀天涯", "1234564879879");
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static List<String> readFileContent() throws IOException,
			UnsupportedEncodingException {
		// String[] chapterArrays=new
		List<String> chapters = new ArrayList<String>();
		File infile = new File("d:/chen.txt");
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(infile), "utf-8"));
		while (in.read() != -1) {
			String target = in.readLine();
			if (target.startsWith("##第")) {
				chapters.add(target);
			}
		}
		return chapters;
	}

	public static List<String> getChapterNames() throws IOException,
			UnsupportedEncodingException {
		List<String> chapterNames = new ArrayList<String>();
		File files = new File("F:/Music"); // 指定文件名及路径
		for (String file : files.list()) {
			File tempFile = new File(files + "\\" + file);
			String fileName = tempFile.getName();
			String suffix = fileName.substring(0, fileName.lastIndexOf("."));
			chapterNames.add(suffix);
		}
		return chapterNames;
	}

	public static void replace(String infilename, String from, String to)
			throws IOException, UnsupportedEncodingException {
		File infile = new File(infilename);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(infile), "gbk"));
		File outfile = new File(infile + ".txt");
		PrintWriter out = new PrintWriter(new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(outfile), "gbk")));
		String reading;
		while ((reading = in.readLine()) != null) {

			out.println(reading.replaceAll(from, to));
		}
		out.close();
		in.close();
		infile.delete();
		outfile.renameTo(infile);
	}
}

 

分享到:
评论

相关推荐

    Web应用安全:XSS通过JavaScript攻击(实验).docx

    - 页面跳转:通过"Replace Content",指定新的URL,使用户页面自动跳转。 这个实验的附加题鼓励你进一步探索Beef-xss提供的更多XSS JavaScript功能,这包括但不限于:键盘记录、屏幕截图、文件上传/下载控制等,以...

    使用nodejs搭建最简单的comet原型实用.pdf

    在CGI时代,"Server-Push"是Comet的一种早期形式,通过multipart/x-mixed-replace Content-Type实现服务器向浏览器推送内容的更新。而现代的Comet则更加灵活,支持HTTP chunked编码,允许服务器以块的形式发送数据,...

    【教学用,C#实现】删除或者替换文件内容

    void ReplaceContent(string filePath, string oldText, string newText) { if (File.Exists(filePath)) { string fileContent = File.ReadAllText(filePath); fileContent = fileContent.Replace(oldText, new...

    DOM,JDOM解析用的JAR包

    4. **修改XML**:你可以通过创建新的`Element`、`Attribute`对象,或者修改现有对象,然后使用`replaceContent()`、`setAttribute()`等方法来更新XML结构。 5. **序列化XML**:使用`org.jdom2.output.Format`和`...

    markup-formatter:NodeJS脚本,用于格式化基于标记的文件(例如HTML,XML等)

    用于格式化基于标记的文件(例如HTML,XML等) markup-formatter.js [options] file...Options: -i --indent &lt;text&gt; use &lt;text&gt; as indent when prettifying (default: 3 spaces) -n --inplace replace content of ...

    Search and Replace

    "Search and Replace" 是一款专为IT专业人士设计的高效文件内容搜索工具,它具有强大的功能,可以在各种类型的文件,包括ZIP压缩包内的文件,进行快速而精确的搜索和替换操作。这款工具对于开发者,尤其是Java开发者...

    ReplaceGoogleCDN, 一个 Chrome 插件:将 Google CDN 替换为国内的。.zip

    ReplaceGoogleCDN 是一款专为 Chrome 浏览器设计的扩展程序,它的主要功能是将网页中引用的 Google Content Delivery Network (CDN) 的资源替换为国内可访问的镜像站点,以解决由于网络限制导致的加载缓慢或无法加载...

    ReplaceGoogleCDN:将 Google CDN 替换为国内的镜像

    标题 "ReplaceGoogleCDN:将 Google CDN 替换为国内的镜像" 指涉的是在遇到因网络限制导致的Google Content Delivery Network (CDN) 访问问题时,如何通过更换为国内镜像来优化加载速度和提高网页访问稳定性。...

    实现只能输入数字的input不用replace方法

    &lt;meta http-equiv="Content-Type" content="text/html; charset=gbk" /&gt; &lt;title&gt;&lt;/title&gt; /** * 只能输入数字0-9 */ function kp() { if (event.keyCode || event.keyCode &gt;= 65) { event.returnValue = ...

    因str_replace导致的注入问题总结

    研究了下replace的注入安全问题。...meta http-equiv=Content-Type content=text/html; charset=utf-8/&gt; &lt;/head&gt; &lt;body&gt; &lt;?php $x=$_GET['x']; $id=str_replace(addslashes($_GET['y']),'',a

    mysql 替换字段部分内容及mysql 替换函数replace()

    MySQL中的替换功能主要涉及到对数据库表中字段内特定字符串的替换操作,这通常通过`REPLACE()`函数来实现。在数据库管理和数据处理中,这个功能非常实用,可以用于更新已存在的数据,使其满足新的格式或者需求。下面...

    postcss-replace:用于替换字符串的PostCSS插件

    content : "{{ author }}" ; } 并得到这个: /* CustomCSS Library v1.3.7 */ . foo { content : "Gridonic" ; } 安装 $ npm install postcss-replace 用法 postcss ( [ require ( 'postcss-replace' ) ] ) 请...

    js replace替换字符串同时替换多个方法

    let article = content.replace(/(|||||)/gi, function($0, $1){ var replacements = { ";height:auto;display:block;"', ";"', ", "&lt;/article&gt;": "&lt;/div&gt;", "&lt;header&gt;": ", "&lt;/header&gt;": "&lt;/div&gt;" }; ...

    Get-ContentFast:Get-Content 的快速替代方案

    Get-Content 的快速替代方案 ####SYNOPSIS Get-Content 的快速替代方案 ####DESCRIPTION Get-Content 是一个缓慢但功能强大的 cmdlet。 Get-ContentFast 是一种更快的替代方案,具有许多相同的功能。 Get-...

    javascript正则表达式使用replace()替换手机号的方法

    本文实例讲述了javascript正则表达式使用replace()替换手机号的方法。分享给大家供大家参考。...meta http-equiv=”content-type” content=”text/html;charset=utf-8″ /&gt; [removed][removed] [removed]

    php 中的str_replace 函数总结

    $cleanedContent = str_replace(array('&lt;p&gt;', '&lt;/p&gt;', ' '), '', $content); echo $cleanedContent; // 输出 This is a paragraph with tags. ``` 多对多替换示例: ```php $content = "This is a &lt;p&gt;paragraph...

    magento2-replace-tools

    删除第三方捆绑的扩展Content Staging模块 删除可选的Content Staging模块 删除可选的核心模块 删除可选的GraphQL模块 删除可选的MSI模块 删除样本数据模块 删除其他目录中列出的所有软件包 请注意,在这些存储库中...

    js 页面刷新location.reload和location.replace的区别小结

    这个标签通常用于meta标签内,并设置content属性,其中的值指定了页面在多少秒后应该自动刷新,并且可以指定刷新时跳转到的URL。这种做法在Web开发中也较为常见,比如在登录或验证页面中,开发者可能希望在一段时间...

    php使用str_replace实现输入框回车替换br的方法

    本文实例讲述了php使用str_replace实现输入框回车替换br的方法,分享给... $content = str_replace(“n”, “ ”, str_replace(” “, ” “, $content));  return $content; } 先替换掉空格,再替换回车,相当

Global site tag (gtag.js) - Google Analytics