有没有一种方法可以将BufferedReader一次性放入一个字符串中,而不是逐行读取?以下是我目前的做法:
BufferedReader reader = null;
try
{
reader = read(filepath);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
String feed = null;
try
{
line = reader.readLine();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
while (line != null)
{
//System.out.println(line);
try
{
line = reader.readLine();
feed += line;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(feed);
read(char[],int,int)
时,它会通过对底层 reader 的连续调用read():int
来填充它的缓冲区。当内部缓冲区被填满时,它会将其中一部分插入到给定的数组中。因此,API 表示,如果这些底层 read 调用中的第一个返回-1
,那么该方法也会返回-1
,因为它是流的结尾。否则(例如,如果第一次读取调用成功,第二次返回-1
),它仍然会返回已读取的字符数。 - bezmax