翻译java语言的软件_java实现英文翻译程序

本文介绍了使用Java编程实现英文到中文的简单翻译程序。通过加载词库文件、分割单词并进行替换,将源文件中的英文转换成中文,并将结果保存到result.txt。主要涉及的技术包括Properties类加载词库、StringTokenizer分割单词以及文件读写操作。
摘要由CSDN通过智能技术生成

本文实例为大家分享了java实现英文翻译程序的具体代码,供大家参考,具体内容如下

1.功能简介

将文本文件中的英文转换为对应的中文

词库如下:

8b43fb861c7202e4fee0b17e8cf17302.png

源文件:

9a8347bce1fca9f01f8fe3f34ff02605.png

翻译后的文件:

0a4d2c6d44d959b2abb0049b44c37710.png

输入源文件路径,将翻译后的内容输出到result.txt文件中。

2.重要技术

(1)如何载入词库文件

因为词库文件是 kry=value的形式,所有可以用到Properties类的load函数

(2)如何将源文件中的一段英文分理处一个个的单词

可以用StringTokenizer类

(3)如何进行翻译

直接用中文替换相应的英文

3.项目结构

02c0c1fbfd9b3eefb98df083a885dba6.png

(4)代码编写

①FileLoader类

/*文件载入类,将源文件中的内容输出到字节数组中*/

package zhidao3_2;

import java.io.FileInputStream;

import java.io.File;

public class FileLoad {

public static byte[] getContent(String fileName)throws Exception{

File file = new File(fileName);

if(!file.exists()){

System.out.println("输入有误,该文件不存在");

}

FileInputStream fis = new FileInputStream(file);

int length = (int)file.length();

byte[] data = new byte[length];

fis.read(data);

fis.close();

return data;

}

}

②TxtTrans类

/*文件翻译,将字节数组变为字符串,分离出其中的单词,然后翻译为对应的汉字,去掉空格,变为字符串*/

package zhidao3_2;

import java.util.StringTokenizer;

import java.util.Properties;

import java.io.*;

public class TxtTrans {

private Properties pps;

public TxtTrans(){

loadCiku();

}

public void loadCiku(){

pps = new Properties();

try{

FileReader fis = new FileReader("g:/ciku.txt");//以字符载入时没有乱码,以字节载入时出现了乱码

pps.load(fis);

fis.close();

}catch(Exception ex){

ex.printStackTrace(System.out);

System.out.println("载入词库时出错");

}

//System.out.println(pps.get("china")) ;

}

public String trans(byte[] data){

String srcTxt = new String(data);

String dstTxt = srcTxt;

String delim = " ,.!\n\t"; //分隔符可以指定

StringTokenizer st = new StringTokenizer(srcTxt,delim,false);

String sub,lowerSub,newSub;

//int i=0;

while(st.hasMoreTokens()){

sub = st.nextToken(); //分割出的一个个单词

lowerSub = sub.toLowerCase();//统一转换为小写,这样可以简化词库

//System.out.println(sub);

newSub = pps.getProperty(lowerSub);

if(newSub != null){ //如果找到了匹配的汉字,则进行替换

dstTxt = dstTxt.replaceFirst(sub, newSub); //只替换第一个,即只替换了当前的字符串,否则容易造成ch我na的例子

//System.out.println(dstTxt);

}

}

return dstTxt.replaceAll(" ", ""); //去掉空格

}

}

③FileOutput类

/*将字符串输出到文件*/

package zhidao3_2;

import java.io.File;

import java.io.FileOutputStream;

public class FileOutput {

public static void output(String text,String fileName)throws Exception{

File file = new File(fileName);

FileOutputStream fos = new FileOutputStream(file);

fos.write(text.getBytes());

fos.close();

}

}

④主函数

package zhidao3_2;

import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) {

String srcFile = JOptionPane.showInputDialog("输入源文件");

try{

byte[] data = FileLoad.getContent(srcFile);

TxtTrans tt = new TxtTrans();

String dString = tt.trans(data);

FileOutput.output(dString, "g:/result.txt");

}catch(Exception ex){

JOptionPane.showMessageDialog(null, "操作异常");

System.exit(1);

}

JOptionPane.showMessageDialog(null, "翻译完毕");

}

}

最后的项目结构如下:

01e448d04f72889688bf779c92feb6ac.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持谷谷点程序。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Bruce Eckel. "Thinking in Java" (4th Edition). Prentice Hall, 2006. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "The Java Language Specification" (3rd Edition). Addison-Wesley Professional, 2005. 3. Herbert Schildt. "Java: A Beginner's Guide" (6th Edition). McGraw-Hill Education, 2014. 4. Cay S. Horstmann and Gary Cornell. "Core Java" (Volume 1 and 2). Prentice Hall, 2013. 5. Joshua Bloch. "Effective Java" (2nd Edition). Addison-Wesley Professional, 2008. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java Concurrency in Practice". Addison-Wesley Professional, 2006. 7. Elliotte Rusty Harold. "Java I/O" (2nd Edition). O'Reilly Media, 2006. 8. Doug Lea. "Concurrent Programming in Java: Design Principles and Pattern" (2nd Edition). Addison-Wesley Professional, 2000. 9. Martin Fowler. "Refactoring: Improving the Design of Existing Code" (2nd Edition). Addison-Wesley Professional, 2018. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "Better, Faster, Lighter Java" (1st Edition). O'Reilly Media, 2004. 中文翻译: 1. Bruce Eckel. "Java编程思想" (第4版). 机械工业出版社, 2008. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "Java语言规范" (第3版). 机械工业出版社, 2006. 3. Herbert Schildt. "Java程序设计程" (第6版). 机械工业出版社, 2015. 4. Cay S. Horstmann and Gary Cornell. "Java核心技术" (卷1和2). 机械工业出版社, 2013. 5. Joshua Bloch. "Effective Java" (第2版). 机械工业出版社, 2009. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java并发编程实践". 机械工业出版社, 2009. 7. Elliotte Rusty Harold. "Java I/O" (第2版). 中国电力出版社, 2014. 8. Doug Lea. "Java并发编程:设计原则与模式" (第2版). 机械工业出版社, 2009. 9. Martin Fowler. "重构:改善既有代码的设计" (第2版). 人民邮电出版社, 2019. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "轻量级Java企业应用开发" (第1版). 机械工业出版社, 2005.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值