利用Lucene把文本的字体格式进行改动,然后输出到一个新的文件里

本文介绍了一个Java程序,该程序可以将文件中的全角字符转换为对应的半角字符,并将处理后的文本保存到新文件中。通过使用HashMap来映射全角与半角字符,程序实现了批量替换的功能。

这里书中写的是charactorProcess(File file, String destFile)

这里被我改成。(String file,  String destFIle)

一个代表现有的文件和要新建的文件


代码:


package com;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;


public class FileRead {
public static String replace(String line){


//创建一个HashMap存储全角和半角字符之间的相应关系
HashMap map = new HashMap();
map.put(",", ",");
map.put("。

", ".");
       map.put("〈", "<");
       map.put("〉", ">");
       map.put("|", "|");
       map.put("《", "<");
       map.put("》", ">");
       map.put("[", "[");
       map.put("]", "]");
       map.put("?", "?

");
       map.put(""", "\"");
       map.put(":", ":");
       map.put("﹑", ",");
       map.put("(", "(");
       map.put(")", ")");
       map.put("【", "[");
       map.put("】", "]");
       map.put("-", "-");
       map.put(" ̄", "~");
       map.put("!", "!");
       map.put("`", "`");
       map.put("1", "1");
       map.put("2", "2");
       map.put("3", "3");
       map.put("4", "4");
       map.put("5", "5");
       map.put("6", "6");
       map.put("7", "7");
       map.put("8", "8");
       map.put("9", "9");
       
       int length = line.length();
       for(int i = 0; i < length; i++){
        String charat = line.substring(i, i + 1);
        if(map.get(charat) != null){
        line = line.replace(charat, (String)map.get(charat));
        }
       }
       return line;

}

public static File charactoProcess(String string, String destFile){
try{
//创建一个输出流,用于写新文件
BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));

//创建一个输入流。用于读取文件
BufferedReader reader = new BufferedReader(new FileReader(string));
String line = reader.readLine();
while(line != null){

//调用replace方法替换全部的全角字符
String newline = replace(line);

//将替换后的String写入新的文件
writer.write(newline);

//写入行分隔符
writer.newLine();
line = reader.readLine();
}

reader.close();
writer.close();

}catch(IOException e){
e.printStackTrace();
}
return new File(destFile);
}


public static void main(String[] args) {
// TODO Auto-generated method stub
FileRead b = new FileRead();
b.charactoProcess("E:\\Lucene项目\\钢铁是怎么样练成的.txt", "E:\\Lucene项目\\目标文件.txt");


}


}

转载于:https://www.cnblogs.com/brucemengbm/p/6723020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值