標籤:exce write auto trace imp close import 入行 pre
這裡書中寫的是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");
}
}
利用Lucene把文本的字型格式進行改動,然後輸出到一個新的檔案裡