转载:readLine()与read()
版权声明:本文为原博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_31057219/article/details/78742653
参考:
readLine() 和 “\r”,”\n” 问题
被readLine()折腾了一把
httpURLConnection-网络请求的两种方式-get请求和post请求
关于BufferefReader.readLine()方法的理解
readLine()
功能:读取一个文本行。
一定要注意:
1、读入的数据要注意有/r或/n或/r/n
2、没有数据时会阻塞,在数据流异常或断开时才会返回null
3、使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞
4、readLine()是一个阻塞函数,当没有数据读取时,就一直会阻塞在那,而不是返回null
5、readLine()只有在数据流发生异常或者另一端被close()掉时,才会返回null值。
6、如果不指定buffer大小,则readLine()使用的buffer有8192个字符。在达到buffer大小之前,只有遇到”/r”、”/n”、”/r/n”才会返回。
7、该方法读取一行文本,当遇到换行符”\n”,回车符”\r”或者回车符后面紧跟着换行符时,该行结束并返回。没有数据时,将会一直处于等待状态。因此在进行网络连接时,应该避免使用该方法。
read()
功能:读取单个字符的个数,如果已经读完的话会返回-1 (其范围从 0 到 65535 )
readLine()示例:
private static String getStringFromInputStream(InputStream a_is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(a_is));
while ((line = br.readLine()) != null) {//如果之前文件为空,则不执 行输出
sb.append(line);
}
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
return sb.toString();
}
//b=bf.read())!=-1 每次都会先读取一个字符出来(不是字节)
//凡是inputstream和outputStream这种抽象类的子类。就是字节流。那么read()读的就是一个字节。
//Reader和Writer则是字符流。所以读的是一个字符
read()示例:
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
bis = new BufferedInputStream(getActivity().getAssets()
.open("movie" + position + ".txt"));
byte[] bytes = new byte[1024];
int i = -1;
while ((i = bis.read(bytes)) != -1) {//单个读取计数,直到结束返回-1
baos.write(bytes, 0, i);
baos.flush();
}
String data = baos.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
readLine测试:
/**
* readLine函数会自动截取"\r","\n"之前的字符串
*/
public class ReaderTest {
public static void main(String[] args) {
try {
String line = "helloworld";//readLine 读出后的长度: 10 readLine读的结果: helloworld
// String line = "hello\rworld";//readLine 读出后的长度: 5 readLine读的结果: hello
// String line = "\rworld";//readLine 读出后的长度: 0 readLine读的结果:
OutputStream out = new FileOutputStream(".//out.txt");
out.write(line.getBytes());
InputStream in = new FileInputStream(".//out.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String str = reader.readLine();
System.out.println("readLine 读出后的长度: " + str.length() + " readLine读的结果: " + str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
read()测试
public class ReaderTestI {
public static void main(String[] args) {
try {
// String line = "helloworld";//helloworld
String line = "hello\rworld";//world
// String line = "\rworld";//world
OutputStream out = new FileOutputStream(".//out.txt");
out.write(line.getBytes());
InputStream in = new FileInputStream(".//out.txt");
byte[] b = new byte[50];
in.read(b, 0, line.length());
for (int i = 0; i < line.length(); i++) {
System.out.print((char) b[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
转载:readLine()与read()的更多相关文章
- C#线程入门---转载
C#中的线程(一)入门 文章系参考转载,英文原文网址请参考:http://www.albahari.com/threading/ 作者 Joseph Albahari, 翻译 Swanky Wu 中 ...
- .Net读取Excel文件时丢失数据的问题 (转载)
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
- 【转载】关于Alipay支付宝接口(Java版)
转载自:http://blog.163.com/lai_chao/blog/static/70340789201412724619514/ 1.alipay 双功能支付简介 2.alipay 提交支付 ...
- 在多台服务器上简单实现Redis的数据主从复制(3)(转载)
转载地址:http://www.cnblogs.com/liping13599168/archive/2011/04/14/2016226.html Redis的主从复制功能非常强大,一个master ...
- Java 输入输出流 转载
转载自:http://blog.csdn.net/hguisu/article/details/7418161 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所 ...
- jsp 微信公众平台 token验证(php、jsp)(转载)
微信公众平台现在推出自动回复消息接口,但是由于是接口内容用的是PHP语言写的,很多地方操作起来让本人这个对java比较熟悉的小伙很别扭,所以仿照PHP的接口代码做了一套jsp语言编写的接口. 首先先把 ...
- 转载-python学习笔记之文件I/O
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- 转载-python学习笔记之输入输出功能读取和写入数据
读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...
- 转载-Python学习笔记之文件读写
Python 文件读写 Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件 ...
随机推荐
- C# 通过api函数GetPrivateProfileString读取ini文件,取不到值
通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...
- 高级数据库技术SQL
- Ubuntu安装bcmath扩展
sudo apt-get install php-bcmath
- 没加载redis类,却可以实例化redis
原因:phpinfo里面已有redis扩展
- [SoapUI] 在SoapUI中通过Groovy脚本执行window命令杀掉进程
//杀Excel进程 String line def p = "taskkill /F /IM EXCEL.exe".execute() def bri = new Buffere ...
- js Array 方法总结
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【Linux】ApacheBench(ab)压力测试工具
AB的简介 ab是apachebench命令的缩写. ab是apache自带的压力测试工具.ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试.比 ...
- BP神经网络的数学常识
输入数据X1-Xn. 输入层和隐层之间的权Wji 隐层的输入数据为:∑iwjixi 隐层的输出数据为:yj = f(∑iwjixi).其中f(x)= 隐层的输入数据为:∑jwkjyj 隐层的输出数据为 ...
- mybatis 使用merge into
前一篇博客,oracle的merge into语法 : oracle merge into语法 mybatis 使用merge into,跟一般的update写法相同: <update id=& ...
- 821. Shortest Distance to a Character
class Solution { public: vector<int> shortestToChar(string S, char C) { int len=S.length(); ve ...