读取URL返回的文本文件 [英] read text file returned by URL
问题描述
此URL回报和放大器;打开文本文件直接,我只是想读的内容我怎么能做到这一点
<一href=\"http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=en&tl=gu&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=this+is+translate+demo\" rel=\"nofollow\">http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=en&tl=gu&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=this+is+translate+demo
我曾尝试
公共静态字符串翻译(字符串SL,TL字符串,字符串文本)抛出IOException
//取
网址URL =新的URL(https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=+
SL +与&amp; TL =+ TL +与&amp;即= UTF-8和; OE = UTF-8和;多分辨率= 1和; OC = 1和; OTF = 2及SSEL = 0&放大器; TSEL = 0&放大器; SC = 1和; q =+
URLEn coder.en code(文字,UTF-8));
Log.d(URL,::+网址);
URLConnection的的URLConnection = url.openConnection();
urlConnection.setRequestProperty(用户代理,其他);
Log.d(URL,开::连接后);
BR的BufferedReader =新的BufferedReader(新的InputStreamReader(urlConnection.getInputStream()));
Log.d(URL,:: BR+ BR);
字符串结果= br.readLine();
br.close();
//解析
Log.d(URL,::+结果);
结果= result.substring(2,result.indexOf(]])+ 1);
StringBuilder的SB =新的StringBuilder();
的String [] =拆分result.split((小于?!\\\\\\\\)\\);
的for(int i = 1; I&LT; splits.length; I + = 8)
sb.append(拆分[I]);
返回sb.toString()代替(\\\\ N,\\ n)的replaceAll(\\\\\\\\()。,$ 1)。;
}
如果您的网址直接打开的文本文件,则该code读取文本文件和打印也是如下:
公共类URLReader {
公共静态无效的主要(字串[] args)抛出异常{ URL =甲骨文新的URL(http://www.oracle.com/);
在的BufferedReader =新的BufferedReader(
新的InputStreamReader(oracle.openStream())); 串inputLine;
而((inputLine = in.readLine())!= NULL)
的System.out.println(inputLine);
附寄();
}
}
This URL return & open text file directly, i just want to read its content how can i do it http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=en&tl=gu&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=this+is+translate+demo
i have tried
public static String translate(String sl, String tl, String text) throws IOException{
// fetch
URL url = new URL("https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +
sl + "&tl=" + tl + "&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=" +
URLEncoder.encode(text, "UTF-8"));
Log.d("URL", ":: "+url);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Something Else");
Log.d("URL", ":: After opening Connection");
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
Log.d("URL", ":: br "+br);
String result = br.readLine();
br.close();
// parse
Log.d("URL", ":: "+result);
result = result.substring(2, result.indexOf("]]") + 1);
StringBuilder sb = new StringBuilder();
String[] splits = result.split("(?<!\\\\)\"");
for(int i = 1; i < splits.length; i += 8)
sb.append(splits[i]);
return sb.toString().replace("\\n", "\n").replaceAll("\\\\(.)", "$1");
}
If Your Url directly open's the Text File then this code reads the TextFile and print's also as follows:
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
这篇关于读取URL返回的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!