一个关于java按行读取的函数如下public String readUntil(StringBuffer sb,InputStream in) {
try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine(); // 读取第一行
while (line!=null) { // 如果 line 为空说明读完了
sb.append(line); // 将读到的内容添加到 buffer 中
sb.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行 **
}
System.err.println(sb.toString());
return sb.toString();
}catch (Exception e) {e.printStackTrace();}
return null;
}不能正常返回读取的字符串
调试时执行到(**)标记的这一行就不执行了
有哪位高手能帮忙解决下?
不胜感激
try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine(); // 读取第一行
while (line!=null) { // 如果 line 为空说明读完了
sb.append(line); // 将读到的内容添加到 buffer 中
sb.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行 **
}
System.err.println(sb.toString());
return sb.toString();
}catch (Exception e) {e.printStackTrace();}
return null;
}不能正常返回读取的字符串
调试时执行到(**)标记的这一行就不执行了
有哪位高手能帮忙解决下?
不胜感激
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((thisLine = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
二楼的方法就是改了一下我的方法的写法而已
没做实际的更改方法执行一段时间后就会打印如下信息
Welcome to Microsoft Telnet Service
login:
Session timed out.Telnet Server has closed the connection但是我希望在打印如下信息后就返回该字符串
Welcome to Microsoft Telnet Service
login:
而不是等到session超时后返回
高手指点
检查下 line的返回值!
明确问题所在,代码没有问题,有可能文件条件不满足或外部传入的参数问题等
但调试到这一行就不执行了
这个不知道是什么原因
try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
int c;
do
{
String line= reader.readLine();
sb.append(line);
sb.append("\n");
}while ((c=reader.read())!=-1);
}catch (Exception e) {e.printStackTrace();}
return null;
}
readLine是会一直阻塞直到流中返回了回车字符,又或者流结束了
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;public class Test
{
public static void main(String args[])
{ InputStream in;
try {
in = new FileInputStream("\\t.txt");
StringBuffer sb=new StringBuffer();
new Test().readUntil(sb, in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String readUntil(StringBuffer sb,InputStream in) {
try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine(); // 读取第一行
while (line!=null) { // 如果 line 为空说明读完了
sb.append(line); // 将读到的内容添加到 buffer 中
sb.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行 **
}
System.out.println(sb.toString());
return sb.toString();
}catch (Exception e) {e.printStackTrace();}
return null;
}
}
//这个程序正确,楼主你怎么了?
public String readUntil(StringBuffer sb,InputStream in) { try { String line; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); ; // 读取第一行 while ((line = reader.readLine())!=null) { // 如果 line 为空说明读完了 sb.append(line); // 将读到的内容添加到 buffer 中 sb.append("\n"); // 添加换行符 line = reader.readLine(); // 读取下一行 ** } System.err.println(sb.toString()); return sb.toString(); }catch (Exception e) {e.printStackTrace();} return null; }
这样试试
while (line!=null) { // 如果 line 为空说明读完了
sb.append(line); // 将读到的内容添加到 buffer 中
sb.append("\n"); // 添加换行符
line=null;//是不是这里要添加这行?
line = reader.readLine(); // 读取下一行 **
}
while (line!=null) 这样判断行么? 与null比较吗?
import java.io.IOException;
import java.io.InputStreamReader;public class ReadLine { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String string = br.readLine();
if(string ==null){
System.out.println(string+"是空值!");
}
if("".equals(string)){
System.out.println(string+"是‘’");
}
System.out.println(string);
}}
我测试过了是你循环的条件不对。
while ("".equals(line)) { // 如果 line 为空说明读完了
相互交流,共同提高,期待你的加入
不过存在如下乱码
如何才能过滤掉这些乱码?
望高手指教
Welcome to Microsoft Telnet Service login: administrator
password: [1;1H*=============================================================== [2;1HWelcome to Microsoft Telnet Server. [3;1H*=============================================================== [4;1HC:\Documents and Settings\Administrator> [5;1H[K[6;1H[K[7;1H[K[8;1H[K[9;1H[K[10;1H[K[11;1H[K[12;1H[K[13;1H[K[14;1H[K[15;1H[K[16;1H[K[17;1H[K[18;1H[K[19;1H[K[20;1H[K[21;1H[K[22;1H[K[23;1H[K[24;1H[K[25;1H[K[4;41H[4;41Hhostname[5;1Hmwgj-wmw[7;1HC:\Documents and Settings\Administrator>
String line = null
while ((line = br.readLine()) != null) {
sb.append(line);
}
/** getStrFromIS
* get string from an InputStream object
* @param is: a nInputStream object
* @param charSet if charSet is null or "", use the system default charSet
* @Return a String of the InputStream.
*/
public static String getStrFromIS(InputStream is,String charSet) throws Exception{
ByteArrayOutputStream os = new ByteArrayOutputStream();
int read;
byte[] bytes = new byte[BUFSIZE]; //BUFSIZE 是一个常量10240=10k
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
is.close();
String str;
if (charSet==null || charSet.length() == 0)
str = os.toString();
else
str = os.toString(charSet);
return str;
}
*===============================================================
Welcome to Microsoft Telnet Server.
*===============================================================
C:\Documents and Settings\Administrator> dir
驱动器 C 中的卷没有标签。
卷的序列号是 BCFD-343D
C:\Documents and Settings\Administrator 的目录
2008-12-21 17:16 <DIR> .
2008-12-21 17:16 <DIR> ..
2008-12-21 12:58 <DIR> .datastudio
2008-12-21 13:13 <DIR> .myeclipse
2008-12-21 13:14 124 .myeclipse.properties
2008-12-20 13:36 <DIR> Bluetooth Software
2008-12-21 13:09 <DIR> Contacts
2008-12-20 19:36 <DIR> Favorites
2008-12-21 13:10 <DIR> My Documents
2008-12-21 17:10 <DIR> 「开始」菜单
2009-02-12 11:27 <DIR> 桌面
1 个文件 124 字节
10 个目录 2,905,075,712 可用字节
C:\Documents and Settings\Administrator>2009-02-12 11:27 <DIR> 桌面
1 个文件 124 字节
10 个目录 2,905,075,712 可用字节
C:\Documents and Settings\Administrator>dir
驱动器 C 中的卷没有标签。
卷的序列号是 BCFD-343D
C:\Documents and Settings\Administrator 的目录
7:16 <DIR> ..
2:58 <DIR> .datastudio
3 <DIR> .myeclipse
1 13:14 124 .myeclipse.properties
0 13:36 <DIR> Bluetooth Software
1 13:09 <DIR> Contacts
0 19:36 <DIR> Favorites
3:10 <DIR> My Documents
8-12-21 17:10 <DIR> 「开始」菜单
2009-02-12 11:27 <DIR> 桌面
1 个文�� 124 字节
10 个目录 2,905,075,712 可用字节
C:\Documents and Settings\Administrator>郁闷
执行第一次的时候没问题
第二次就出现问题了
我每次读取一定长度的流,在如下情况就会堵死
流的长度是8,我每次读4,第二次读完后读取的长度是4,然后读第三次,正常的情况我认为是返回值为-1,这样就不继续读了,可是事实是第三次就堵死了