上題目:
python版本
#關于count 函數,主要計算字元串在字元中出現的次數,參考https://www.runoob.com/python/att-string-count.html
#關于replace 函數,主要把字元串做替代,參考 https://www.runoob.com/python/att-string-replace.html
class WordFinder:
replacedic = {"utilize":"use","should":"can"}
filename =""
def Getinput(this):
filename = input("Please provide the name of the file")
this.filename = filename
def Transform(this):
fo = open(this.filename+"cg","w")
with open(this.filename,'r') as f:
line = f.readline()
number = 0
while line:
#print(line.replace('utilize','use'))
#fo.write(line.replace('utilize','use'))
str,num = this.replace(line)
number = number + num
fo.write(str)
line = f.readline()
f.close()
fo.close()
print("We have produce {0} number of replacements".format(number))
def replace(this,str):
num = 0
for key in this.replacedic:
num = num + str.count(key)
str = str.replace(key,this.replacedic[key])
# print("we have produce {0} replacements".format(num))
return str,num
lesson45 = WordFinder()
lesson45.Getinput()
lesson45.Transform()
感覺python版本靈活一點,寫的比較快。
看到一共做了5次修改,修改後的文檔在1.txtcg 中
java 版本
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.*;
/*
// 1. 周遊hashmap 資料 : 參考 https://www.runoob.com/java/java-hashmap.html
* for (Integer i : Sites.keySet()) {
System.out.println("key: " + i + " value: " + Sites.get(i));
}
// 2. 讀檔案
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine() )!= null)
{
System.out.println(line);
}
// 3.寫檔案
BufferedWriter out = new BufferedWriter(new FileWriter(filename+"java"));
out.write(str+"\n");
out.close();
//4.查找檔案
**
* 擷取指定字元串出現的次數
*
* @param srcText 源字元串
* @param findText 要查找的字元串
* @return
public static int appearNumber(String srcText, String findText) {
int count = 0;
Pattern p = Pattern.compile(findText);
Matcher m = p.matcher(srcText);
while (m.find()) {
count++;
}
return count;
}
*/
public class WordFinder {
HashMap<String,String> replacedic = new HashMap<String,String>();
String filename = "";
Scanner sc = new Scanner(System.in);
int replacenumber=0;
void GetInput()
{
System.out.println("Please provide the name of the file");
filename = sc.nextLine();
}
void Transform() throws IOException{
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
// 這裡增加内容,通過字柄寫入内容
BufferedWriter out = new BufferedWriter(new FileWriter(filename+"java"));
while((line = br.readLine() )!= null)
{
//System.out.println(line);
String str = this.replacemystr(line);
//System.out.println(str);
out.write(str+"\n");
}
fis.close();
isr.close();
out.close();
System.out.print("Totally we have modified " + replacenumber +" files");
}
String replacemystr(String str){
replacedic.put("utilize","use");
replacedic.put("should","can");
for (String a: replacedic.keySet()){
replacenumber = replacenumber + appearNumber(str,a);
//System.out.println("The replacementnumber is " + replacenumber);
str = str.replace(a,replacedic.get(a));
}
return str;
}
public static int appearNumber(String srcText, String findText) {
int count = 0;
Pattern p = Pattern.compile(findText);
Matcher m = p.matcher(srcText);
while (m.find()) {
count++;
}
return count;
}
public static void main(String[] args) throws IOException{
WordFinder lesson45 = new WordFinder();
lesson45.GetInput();
lesson45.Transform();
}
}
效果圖:
java 版本利用周末弄了下,感覺還是比較麻煩,代碼比較長一點
java版本
運作效果圖