博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用Lucene把文本的字体格式进行改动,然后输出到一个新的文件里
阅读量:4618 次
发布时间:2019-06-09

本文共 2017 字,大约阅读时间需要 6 分钟。

这里书中写的是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

你可能感兴趣的文章
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>
android编译遇到问题修改
查看>>
解决Ubuntu18.04.2远程桌面Xrdp登录蓝屏问题
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>