读取txt文件实现搜索替换功能

主要是判断文件编码,根据返回编码类型读取文件,对文件内容搜索,找到要改的内容,进行替换。

1、判断编码类

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.UnicodeDetector;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

public class CharsetUtil {
	/**
	 * 检查文件的编码格式
	 * @param path 待查文件路径
	 * @return String文件的编码名
	 */
	public static String getCharset(String path){
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		detector.add(JChardetFacade.getInstance());
		detector.add(ASCIIDetector.getInstance());
		detector.add(UnicodeDetector.getInstance());
		File file = new File(path);
		Charset charset = null;
		try {
			charset = detector.detectCodepage(file.toURL());
			if(charset!=null){
				return charset.name();
			}else{
				return null;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}

2、读文件与写文件类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class IOUtils {
	/**
	 * 读取txt文件
	 * @param txtpath
	 * @return StringBuffer 返回读到内容
	 */
	public static StringBuffer textRead(String txtpath,String charset) {
		BufferedReader br;
		FileInputStream fis = null;
		InputStreamReader isr = null;
		try {
			fis = new FileInputStream(txtpath);
			isr = new InputStreamReader(fis,charset);
			br = new BufferedReader(isr);
			StringBuffer textbuffer = new StringBuffer();
			String temp = br.readLine();
			while (temp != null) {
				textbuffer.append(temp + "\r\n");
				temp = br.readLine();
			}
			fis.close();
			isr.close();
			br.close();
			return textbuffer;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存txt
	 * 
	 * @param txtpath
	 *            保存路径
	 * @param txtbuffer
	 *            要存内容
	 */
	public static void txtSave(String txtpath, StringBuffer txtbuffer,String charset) {
		BufferedWriter bw = null;
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos = new FileOutputStream(txtpath);
			osw = new OutputStreamWriter(fos,charset);
			bw = new BufferedWriter(osw);
			bw.write(txtbuffer.toString());
			bw.flush();
			fos.close();
			osw.close();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3、查找替换类

import utils.CharsetUtil;
import utils.IOUtils;

public class TxtReplace {
	/**
	 * 替换TXT中指定字符串
	 * 
	 * @param str
	 *            要找的字符串
	 * @param rpstr
	 *            替换用的字符串
	 * @param txtPath
	 *            txt文件路径
	 */
	public static void txtReplace(String str, String rpstr, String txtpath) {
		String charset = CharsetUtil.getCharset(txtpath);
		StringBuffer txtbuffer = new StringBuffer(IOUtils.textRead(txtpath,charset));
		int i = 0;
		int j = 0;
		while (txtbuffer.indexOf(str, i) != -1) {
			int start = txtbuffer.indexOf(str, i);
			int end = start + str.length();
			txtbuffer.replace(start, end, rpstr);
			i++;
			j++;
		}
		System.out.println("------------");
		System.out.println("  "+j + "处替换!");
		System.out.println("------------");
		IOUtils.txtSave(txtpath, txtbuffer,charset);
	}
}

4、测试类

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		TxtReplace.txtReplace(str,rpstr,args[2]);
		TxtReplace.txtReplace("str","rpstr","F:/xx.txt");
	}
}


在读取文件的时候如果要设置编码的话就要用InputStreamReader 和OutputStreamWriter类有设置编码的构造方法

还要引入 cpdetector_1.0.8.jar .jar 和jchardet-1.0.jar不然会报错 这两个是第一个判断文件编码类需要引入的

如果不需要就可以不用第一个类,把后面2部里的charset写死,比如用utf-8ti替换


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值