首页 技术 正文
技术 2022年11月21日
0 收藏 854 点赞 4,261 浏览 1906 个字

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

}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777