本文实例讲述了java实现简单的英文文本单词翻译器功能。分享给大家供大家参考,具体如下:
直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package fanyi;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.unsupportedencodingexception;
import java.util.scanner;
import java.util.stringtokenizer;
public class text {
public static void handle(string estring) throws ioexception {
stringtokenizer st = new stringtokenizer(estring, ",!' '.;" );
while (st.hasmoreelements()) {
string stext;
stext = st.nextelement().tostring();
//system.out.println(stext);
string encoding= "gbk" ;
string filepath= "fanyi\\src\\fanyi\\word.txt" ;
file file= new file( "g:\\workspace4\\fanyi\\src\\fanyi\\word.txt" );
// system.out.println("2222");
if (file.isfile() && file.exists()){ //判断文件是否存在
//system.out.println("1111");
inputstreamreader read = new inputstreamreader( new fileinputstream(file),encoding); //考虑到编码格式
bufferedreader bufferedreader = new bufferedreader(read);
string linetxt = null ;
while ((linetxt = bufferedreader.readline()) != null ){
//system.out.println(linetxt);
//system.out.println("333");
if (linetxt.tostring().equals(stext)){
system.out.println(stext + bufferedreader.readline());
}
}
read.close();
}
}
}
public static void main(string[] args) throws ioexception {
scanner sc = new scanner(system.in);
system.out.println( "请输入英文文本:" );
string etext = sc.nextline();
handle(etext);
//system.out.println(etext);
}
}
|
文件结构如下:
运行结果:
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/spflinux/article/details/77836988