利用Lucene把文本的字型格式進行改動,然後輸出到一個新的檔案裡

來源:互聯網
上載者:User

標籤: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把文本的字型格式進行改動,然後輸出到一個新的檔案裡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.