- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一些代码
public static void main(String[] args) throws HttpException, IOException,
JSONException {
// TODO Auto-generated method stub
try {
URL murl = new URL(
"http://www.baidu.com/link?url=NaethV_J2hSPVx_OdPlHk73964mU4LcwWkJmVUV4vIkuCXRf1y09ufRZVwkHJqSAa2mMSCoTLYVhGv2AyV_04_");
HttpURLConnection conn = (HttpURLConnection) murl.openConnection();
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36");
conn.setConnectTimeout(10000);
conn.connect();
String strencoding = null;
Map<String, List<String>> map = conn.getHeaderFields();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
String key = null;
String tmp = null;
while (iterator.hasNext()) {
key = iterator.next();
tmp = map.get(key).toString().toLowerCase();
if (key != null && key.equals("Content-Type")) {
System.out.println(tmp);
int m = tmp.indexOf("charset=");
if (m != -1) {
strencoding = tmp.substring(m + 8).replace("]", "");
}
}
}
strencoding = strencoding == null ? "UTF-8" : strencoding;
conn.getResponseCode();
// conn.connect();
String href = conn.getURL().toString();
System.out.println(href);
href = href.replace("http://", "");
try {
href = href.split("/")[0];
} catch (Exception eee) {
}
/*
* ParseDomainName pdn = new ParseDomainName(href);
* System.out.println("Your host IP is: " +
* pdn.getMyIP().getHostAddress());
* System.out.println("The Server IP is :" +
* pdn.getServerIP().getHostAddress()); // InputStream inputstream =
* conn.getInputStream();
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), strencoding));
String lines;
int i = 1;
while ((lines = reader.readLine()) != null) {
if (lines.toLowerCase().indexOf("charset") > 0) {
System.out.println(lines);
String strtmp = lines;
int inttmp = strtmp.indexOf("charset");
if (inttmp > -1) {
System.out.println(strtmp.length());
strencoding = strtmp
.substring(inttmp + 7, strtmp.length())
.replace("=", "").replace("/", "")
.replace("\"", "").replace("\'", "")
.replace(" ", "").replace("<", "")
.replace(">", "");
break;
}
}
i++;
}
reader.mark(0);
reader.reset();
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), strencoding));
while ((lines = reader.readLine()) != null) {
System.out.println(i + " " + lines);
if (lines.toLowerCase().indexOf("icp") > 0) {
// System.out.println(i + " " + lines);
}
i++;
}
System.out.println(i + "---" + strencoding);
reader.close();
conn.disconnect();
} catch (Exception e2) {
e2.printStackTrace();
}
}
最后一个while
循环,在第一个循环中,我检查页面字符集并中断,然后我重置阅读器,然后再次readLine
,但在第二个循环中,它从第一个循环结束的位置开始。
有时它会打印这样的结果:
有时在第二个循环中不读取任何内容,如下所示:
那么问题出在哪里呢?
最佳答案
您必须在第一个 while 循环之前调用 reader.mark() ; reader.mark() 本质上保存了阅读器的当前位置,以便您在调用 reader.reset() 时可以返回到该位置。
您也不想将 0 传递给 reader.mark()。请参阅以下参数的 java 规范:
readAheadLimit - 在保留标记的同时限制可以读取的字符数。读取字符达到或超过此限制后尝试重置流可能会失败。大于输入缓冲区大小的限制值将导致分配一个大小不小于限制的新缓冲区。因此应谨慎使用较大的值。
(也就是说,传入0是没有用的,需要传入一个大于mark()和reset()之间读取的字符数的数字)。
关于java - 如何不重置BufferedReader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794330/
我可以做这个科目吗?我的意思是: BufferedReader reader1 = new BufferedReader(new FileReader(new File("file"))); Bu
这个问题已经有答案了: Unhandled Exception Type IOException [duplicate] (4 个回答) Java: import statement vs fully
PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true); BufferedReader in = ne
我是java新手。我想从 java 启动的进程中读取标准输出。我在谷歌搜索中得到了以下代码: Runtime rt = Runtime.getRuntime(); Process pr
这个问题已经有答案了: Stream closed and not reopened - Java (2 个回答) Closing Reader/Stream in Java [duplicate]
我有一个片段如下: Process proc = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(
1. 概述 在这个例子中,我们将使用一个BufferedReader类来读取一个名为 "sample.txt "的文件。 BufferedReader类是用来从基于字符的
我正在 youtube 上查看有关 BufferedReader 的教程 https://www.youtube.com/watch?v=yofFVbARIRU 我完全按照他的方式编写代码,但我无法让
我正在尝试编写一个方法来获取一个多行制表符分隔的文件,并将该文件的内容作为字符串数组的数组列表返回(每一行都是一个字符串[],每个这样的字符串[]都是一个元素一个数组列表)。我的问题是,我无法判断输出
我需要从 Java InputStream 中读取以零结尾的字符串。 有没有类似BufferedReader.readLine()的读取零结尾字符串的方法? 最佳答案 package com; imp
String str = ""; try { BufferedReader br = new BufferedReader(new FileRe
我用下面的样式用BufferedReader读取文件 try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
我正在编写一些使用clojure.async的函数,以从读取/写入套接字抽象出来。我的目的是可以将值放入 channel 中以进行写入,并从要读取的 channel 中弹出。这样,用户无需担心Read
我正在尝试使用缓冲读取器读取文件,但有时它会跳过一行中的第一个字符。这是我正在阅读的文件: http://files.moonmana.com/forums/Rectangle.h 这是我得到的结果:
我是 Java 新手,我正在尝试使用此类返回 "test.csv" 的内容。这只在我使用本地文件的路径时才有效。 public class CSVtoArray2 { public stati
我正在使用 BufferedReader.readLine() 逐行读取文本文件,但突然它不读取整行,而是只读取仅第一个字符串 示例:如果文本文件中的第一行是: [98.0,20.0,-65.0]
读完整个字符串后,读者会停留在 while 中,甚至不会抛出异常。我正在通过curl 向服务器发送请求。 我尝试更改 curl 的内容类型,更改字符串的内容,并使用另一种方式读取输入,例如扫描仪,但总
我正在使用 BufferedReader 从 ma url 获取数据。 URL url = new URL("http://"); BufferedReader in = new Buffered
我有一个缓冲阅读器,由于某种原因,它不会从我从客户端发送的打印流中读取文本。这是每次 line = in.readline 时失败的点 我也检查过并且服务器已连接。 这是错误 java.net.Soc
第一次这个循环迭代效果很好,但在我按字符“y”以重复之后,下次显示时,它不会让我输入另一个名称。我不知道是什么原因造成的,但是当你需要清除输入缓冲区时,它让我想起了 C。 任何帮助我们都将不胜感激。
我是一名优秀的程序员,十分优秀!