readLine()与read()

2023-12-23 05:08
文章标签 read readline

本文主要是介绍readLine()与read(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考:
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) {//单个读取计数,直到结束返回-1baos.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";//helloworldString line = "hello\rworld";//world
//            String line = "\rworld";//worldOutputStream 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()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526813

相关文章

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef‘ of undefined“

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef’ of undefined” 最近用vue做的一个界面,引入了一个子组件,在父组件中调用子组件的方法时,报错提示: [Vue warn]: Error in v-on handler: “TypeError: Cannot read property ‘methods

[轻笔记] pip install : Read timed out. (closed)

添加超时参数(单位秒) pip --default-timeout=10000 install ${package_name}

Cannot read property ‘length‘ of null while opening vscode terminal

同一问题地址:Cannot read property ‘length’ of null while opening vscode terminal 问题描述 One day, 我在ubuntu 18.04下用vscode打开一个项目,并想和往常一样在vscode使用终端,发现报错Cannot read property 'length' of null。 解决 打开setting.jso

王立平--Failed to push selection: Read-only file system

往android模拟器导入资源,失败。提示:只读文件、 mnt是只读文件。应点击sdcard,,在导入

uniapp微信小程序开发踩坑日记:Pinia持久化报错Cannot read property ‘localStorage‘ of undefined

插件默认使用 localStorage 实现持久化,小程序端不兼容,需要替换持久化 API import { defineStore } from 'pinia'   export const useCommonStore = defineStore('pack-store', {state: (): State => ({wwInfo: {},globalData: {},timerLoc

Python文件读写readline()、readlines()、CSV库、pandas库

1.readline() .readline()每次只读取一行,通常比 .readlines()慢得多;仅当没有足够内存可以一次读取整个文件时,才应该使用.readline()。  f = open('poem.txt','r') result = list() for line in open('poem.txt'): line = f.readline() print line

macos 系统文件操作时提示 Read-only file system 解决方法

这个情况是因为文件系统为只读, 需要我们执行一下命令重新将系统文件挂载为读写模式, 命令如下: sudo mount -uw / 这里的 mount 就是硬盘挂载命令, 后面的 -uw选项说明如下, 最后的 / 表示的是跟目录, 可以指定要修改的挂载路径,也可以默认. -u     -u标志表示应更改已装载文件系统的状态。上述任何选项(-o选项)都可以更改;文件系统也可以从只读更改为读写,

redis被攻击redis READONLY You can‘t write against a read only slave.

redis 日志路径 /var/log/redis 拿下来后发现有这种错误 Operation now in progress 可能是网络断开导致, 查找redis whereis redis 修改 vim /etc/redis.conf 大概在300行 下面代码yes改no slave-read-only no 重启redis sudo systemctl restart

【LINUX】“dmesg: read kernel buffer failed: Operation not permitted“ 错误

出现 “dmesg: read kernel buffer failed: Operation not permitted” 错误通常是因为当前用户没有权限读取内核日志缓冲区 这可以通过修改内核参数 kernel.dmesg_restrict 来解决。 你可以尝试以下命令来允许非特权用户读取内核日志: sudo sysctl -w kernel.dmesg_restrict=0 这个命令

error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL_ errno 10054解决方法

error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054解决方法 不敢保证一定能解决,通过搜索多方博客尝试寻找解决方案,最后clone成功。(PS:不保证能成功) http://www.wangxianfeng.cn/wordpress/2018/07/14/git使用过程中常见错误解决/ https: