admin管理员组

文章数量:1130349

package lastwork;
import java.io. * ; 
import java. * ; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class EnToChinese { 

//定义互译语言对常数变量,符合google页面相关对译语言对的值 
public static final String LANGPAIR_CN_EN = "zh-CN|en"; // 汉语到英语 
public static final String LANGPAIR_EN_CN = "en|zh-CN" ; // 英语到汉语 
public static final String LANGPAIR_EN_JA ="en|ja" ; // 英语到日语 
static String nameidentifile;
//google在线翻译引擎url 
static final String engineUrl ="http://translate.google/translate_t" ; 
/** */ /** 
* 利用google在线翻译引擎实现翻译,并获取翻译内容 
* @param translateText 要翻译的文本内容 
* @param langpair 对译语言的值对,如en|ja是由英语翻译到日语 
* @throws Exception  
*/ 
public static String translate(String translateText,String langpair) { 
//text是google翻译页面提交时对于欲翻译文字的变量名 
//langpair是google翻译页面提交时对于采用何种互对语言的变量名 
String urlstr=""; 


//拼接URL串
try { 
urlstr = engineUrl + "?text=" + encodeText(translateText)+ "&langpair=" + langpair; 
} catch (Exception e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 



//将Sting 的URL串转换成URL
URL url = null; 
try { 
url = new URL(urlstr); 
} catch (MalformedURLException e) {
//TODO Auto-generated catch block 
e.printStackTrace(); 



//创建URLConnection连接
URLConnection connection = null; 
try { 
connection = (HttpURLConnection)url.openConnection(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

//用来设置连接参数,一定要写
connection.setRequestProperty("User-agent" , " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; SE 2.X MetaSr 1.0)" ); 
//设置语言环境,一定要写,开始就是没写,结果总是乱码
connection.setRequestProperty("Accept-Language" , "zh-CN" );




try { 
connection.connect(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

//这个没什么好说的,把请求得到的响应流装到String中
BufferedReader in = null; 
try { 
in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"utf-8")); 
} catch (UnsupportedEncodingException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 
} // 使用指定编码接收数据 
String line = null ; 
StringBuilder sb = new StringBuilder(); 
try { 
while ((line = in.readLine()) != null ) { 
sb.append(line); 



} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

try { 
in.close(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 



//getContent方法是从得到的String中把翻译结果找出来
String translation = getContent(sb.toString()); 
return translation; 

/** */ /** 
* 从获得的源文件中剥取翻译内容 
* 分析google翻译生成的html源码来看 
* 翻译内容被置于 
和 
标签之间 
* @param htmltext 获得的网页源代码 
*/ 
private static String getContent(String htmltext) {


String info = "";
//这个是将翻译结果从响应中截出来的方法
info=htmltext.split("TRANSLATED_TEXT=\'")[1].split("\'")[0]; 


return info; 



//将文本进行URL编码 
private static String encodeText(String text) throws Exception { 
String str = java.URLEncoder.encode(text); 
return str; 



public static void readFileByLines(String fileName) throws IOException {


File file = new File(fileName);
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(file));
    String tempString = null;
    // 一次读入一行,直到读入null为文件结束
    while ((tempString = reader.readLine()) != null) {
    System.out.println(translate(tempString, "en|zh-CN"  ));    
    }
    reader.close();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e1) {
        }
    }
}
}
public static void main(String[] args) throws IOException 
{
EnToChinese engine = new EnToChinese(); 
String langpair = "en|zh-CN" ; 
String text = "china"; 
System.out.println(engine.translate(text, langpair));
engine.readFileByLines("C:\\Users\\zhenzhang\\Desktop\\test\\asdEn.txt");
}

记着一定要联网


package lastwork;
import java.io. * ; 
import java. * ; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class EnToChinese { 

//定义互译语言对常数变量,符合google页面相关对译语言对的值 
public static final String LANGPAIR_CN_EN = "zh-CN|en"; // 汉语到英语 
public static final String LANGPAIR_EN_CN = "en|zh-CN" ; // 英语到汉语 
public static final String LANGPAIR_EN_JA ="en|ja" ; // 英语到日语 
static String nameidentifile;
//google在线翻译引擎url 
static final String engineUrl ="http://translate.google/translate_t" ; 
/** */ /** 
* 利用google在线翻译引擎实现翻译,并获取翻译内容 
* @param translateText 要翻译的文本内容 
* @param langpair 对译语言的值对,如en|ja是由英语翻译到日语 
* @throws Exception  
*/ 
public static String translate(String translateText,String langpair) { 
//text是google翻译页面提交时对于欲翻译文字的变量名 
//langpair是google翻译页面提交时对于采用何种互对语言的变量名 
String urlstr=""; 


//拼接URL串
try { 
urlstr = engineUrl + "?text=" + encodeText(translateText)+ "&langpair=" + langpair; 
} catch (Exception e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 



//将Sting 的URL串转换成URL
URL url = null; 
try { 
url = new URL(urlstr); 
} catch (MalformedURLException e) {
//TODO Auto-generated catch block 
e.printStackTrace(); 



//创建URLConnection连接
URLConnection connection = null; 
try { 
connection = (HttpURLConnection)url.openConnection(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

//用来设置连接参数,一定要写
connection.setRequestProperty("User-agent" , " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; SE 2.X MetaSr 1.0)" ); 
//设置语言环境,一定要写,开始就是没写,结果总是乱码
connection.setRequestProperty("Accept-Language" , "zh-CN" );




try { 
connection.connect(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

//这个没什么好说的,把请求得到的响应流装到String中
BufferedReader in = null; 
try { 
in = new BufferedReader( new InputStreamReader(connection.getInputStream(),"utf-8")); 
} catch (UnsupportedEncodingException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 
} // 使用指定编码接收数据 
String line = null ; 
StringBuilder sb = new StringBuilder(); 
try { 
while ((line = in.readLine()) != null ) { 
sb.append(line); 



} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 

try { 
in.close(); 
} catch (IOException e) { 
//TODO Auto-generated catch block 
e.printStackTrace(); 



//getContent方法是从得到的String中把翻译结果找出来
String translation = getContent(sb.toString()); 
return translation; 

/** */ /** 
* 从获得的源文件中剥取翻译内容 
* 分析google翻译生成的html源码来看 
* 翻译内容被置于 
和 
标签之间 
* @param htmltext 获得的网页源代码 
*/ 
private static String getContent(String htmltext) {


String info = "";
//这个是将翻译结果从响应中截出来的方法
info=htmltext.split("TRANSLATED_TEXT=\'")[1].split("\'")[0]; 


return info; 



//将文本进行URL编码 
private static String encodeText(String text) throws Exception { 
String str = java.URLEncoder.encode(text); 
return str; 



public static void readFileByLines(String fileName) throws IOException {


File file = new File(fileName);
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(file));
    String tempString = null;
    // 一次读入一行,直到读入null为文件结束
    while ((tempString = reader.readLine()) != null) {
    System.out.println(translate(tempString, "en|zh-CN"  ));    
    }
    reader.close();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e1) {
        }
    }
}
}
public static void main(String[] args) throws IOException 
{
EnToChinese engine = new EnToChinese(); 
String langpair = "en|zh-CN" ; 
String text = "china"; 
System.out.println(engine.translate(text, langpair));
engine.readFileByLines("C:\\Users\\zhenzhang\\Desktop\\test\\asdEn.txt");
}

记着一定要联网


本文标签: 英文中文