把文件中文字符替换为英文字符.

luozhangwen 2010-05-31 04:02:45

package com.lzw;

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;
import java.util.Map;

public class CharReplaceProcess
{
public static CharReplaceProcess INSTANCE = new CharReplaceProcess();

private Map<String, String> map = new HashMap<String, String>();


public Map<String, String> getReplaceChar()
{
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("!", "!");
map.put("‵", "'");
map.put("①", "1");
map.put("②", "2");
map.put("③", "3");
map.put("④", "4");
map.put("⑤", "5");
map.put("⑥", "6");
map.put("⑦", "7");
map.put("⑧", "8");
map.put("⑨", "9");
return map;
}


public void replaceProcess(String srcFilePath, String descFilePath)
{
BufferedReader br = null;
BufferedWriter bw = null;
try
{
File srcFile = new File(srcFilePath);
File descFile = new File(descFilePath);
br = new BufferedReader(new FileReader(srcFile));
bw = new BufferedWriter(new FileWriter(descFile));

String line = null;
while((line = br.readLine()) != null)
{
String replaceLine = this.replace(line);
bw.write(replaceLine);
bw.newLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
try
{
bw.close();
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}

private String replace(String line)
{
Map<String, String> map = new HashMap<String, String>();
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;
}
}

...全文
311 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhangwen 2010-07-20
  • 打赏
  • 举报
回复

package test.InputStream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class TestBufferedInputStream
{

public static void main(String[] args)
{
String str = readTxtByByteStream("D:/demo/T.txt");
System.out.println(str);
}



/**
* 使用字节流读中文,不支持UTF-8
* @param filePath
* @return
*/
public static String readTxtByByteStream(String filePath)
{
try
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));

int prePart = -1;
boolean nextPart = false;
int len = -1;

StringBuffer sb = new StringBuffer(500);
while ((len = bis.read()) != -1)
{

//请复制这个符号"–" 与 "-"不一样
if (nextPart == true || (128 <= len && len <= 256))
{
if (nextPart == false)
{
nextPart = true;
}
else
{
nextPart = false;
byte[] data = new byte[] { (byte) prePart, (byte) len };

sb.append(new String(data));
}
}
else
{
sb.append(new String(String.valueOf((char) len)));
}
prePart = len;
}
bis.close(); //close the stream must in finaly !
return sb.toString();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}

GOODlivelife 2010-05-31
  • 打赏
  • 举报
回复
每天接分努力升星星!
luozhangwen 2010-05-31
  • 打赏
  • 举报
回复
上边有点问题. 以下更正

package com.lzw;

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;
import java.util.Map;

public class CharReplaceProcess
{
public static CharReplaceProcess INSTANCE = new CharReplaceProcess();

private Map<String, String> map = new HashMap<String, String>();

public static void main(String[] args)
{
CharReplaceProcess.INSTANCE.replaceProcess("D:book.txt", "D:BOOK-desc.txt");
}
public Map<String, String> getReplaceCharsMap()
{
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("!", "!");
map.put("‵", "'");
map.put("①", "1");
map.put("②", "2");
map.put("③", "3");
map.put("④", "4");
map.put("⑤", "5");
map.put("⑥", "6");
map.put("⑦", "7");
map.put("⑧", "8");
map.put("⑨", "9");
return map;
}


public void replaceProcess(String srcFilePath, String descFilePath)
{
BufferedReader br = null;
BufferedWriter bw = null;
try
{
File srcFile = new File(srcFilePath);
File descFile = new File(descFilePath);
br = new BufferedReader(new FileReader(srcFile));
bw = new BufferedWriter(new FileWriter(descFile));

String line = null;
while((line = br.readLine()) != null)
{
String replaceLine = this.replace(line);
bw.write(replaceLine);
bw.newLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally{
try
{
bw.close();
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}

private String replace(String line)
{
Map<String, String> map = this.getReplaceCharsMap();

int length = line.length();
for (int i = 0; i < length; i++)
{
String charat = line.substring(i, i + 1);
if (map.containsKey(charat))
{
line = line.replace(charat, (String) map.get(charat));
}
}

return line;
}
}

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧