java string io_五分钟带你认识StringIO、BytesIO

open()对文件进行读写已经很熟了,那么内存上的读写是怎么样的呢? 大邓对此只是了解了读写的皮毛,对CS基本不懂,但记忆力好,多动手多回顾就是了。好脑子不如多敲代码,敲代码不如多写文章分享,时刻记录总结。嘿嘿,你们成了我烂笔头的见证者,实际上不懂也没关系,跟大邓一样,记住就好了。时间久了不懂也就装懂了,哈哈。

什么IO?

I/O操作就是在内存上写读(输入输出)的意思。

一、StringIO

StringIO就是在内存上读写str。

1.1写入-平常咱们常用的写入

f = open(file,'w')

f.write(data)

1.2写入-内存中的写入

而内存写入数据,类似与此

from io import StringIO

fi = StringIO()

fi.write('write StringIO')

#获取fi的值

print(fi.getvalue())

write StringIO

1.3读取-咱们常用的读取

读取StringIO,跟平常咱们用的类似。先看下平常咱们的读取

f = open(file,'r')

f.read()

1.4读取-内存中的读取

在内存中读取stringIO,如下

from io import StringIO

fo = StringIO('read StringIO')

print(fo.read())

read StringIO

二、BytesIO

和StringIO类似,这里在内存上读写的是二进制数据。BytesIO

from io import BytesIO

#写入

fbi = BytesIO()

fbi.write(b'write bytesIO')

print(fbi.getvalue())

b'write bytesIO'

#读取

fbo = BytesIO(b'read bytesIO')

print(fbo.read())

b'read bytesIO'

public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值