23,404
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
}
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;
}
}