当前位置:编程学习 > JAVA >>

JAVA中几种读取文件字符串的效率哪个比较高?

JAVA的IO还真是博大精深啊,条条大路通罗马,写法也是天女散花。
网上搜刮了半天, 请教一下资深的JAVA人士对读取文件字符串的看法

如下4中方式,有更好的也请帖出来,

谢过~~~~~~~~~

方式一

/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
* 当然也是可以读字符串的。
*/
/* 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/
public String readString1()
{
try
{
//FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。 
FileInputStream inStream=this.openFileInput(FILE_NAME);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=-1;
while( (length = inStream.read(buffer) != -1)
{
bos.write(buffer,0,length);
// .write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream.
//  当流关闭以后内容依然存在
}
bos.close();
inStream.close();
return bos.toString();
// 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么?
// return new String(bos.toByteArray(),"UTF-8");
}
}


方式二


// 有人说了 FileReader  读字符串更好,那么就用FileReader吧
// 每次读一个是不是效率有点低了?
private static String readString2()
{
StringBuffer str=new StringBuffer("");
File file=new File(FILE_IN);
try {
FileReader fr=new FileReader(file);
int ch = 0;
while((ch = fr.read())!=-1 )
{
System.out.print((char)ch+" "); 
}
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("File reader出错");
}
return str.toString();
}


方式三


/*按字节读取字符串*/
/* 个人感觉最好的方式,(一次读完)读字节就读字节吧,读完转码一次不就好了*/
private static String readString3()
{
String str="";
File file=new File(FILE_IN);
try {
FileInputStream in=new FileInputStream(file);
// size  为字串的长度 ,这里一次性读完
int size=in.available();
byte[] buffer=new byte[size];
in.read(buffer);
in.close();
str=new String(buffer,"GB2312");
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
e.printStackTrace();
}
return str;
}


方式四


/*InputStreamReader+BufferedReader读取字符串  , InputStreamReader类是从字节流到字符流的桥梁*/
/* 按行读对于要处理的格式化数据是一种读取的好方式 */
private static String readString4()
{
int len=0;
StringBuffer str=new StringBuffer("");
File file=new File(FILE_IN);
try {
FileInputStream is=new FileInputStream(file);
InputStreamReader isr= new InputStreamReader(is);
BufferedReader in= new BufferedReader(isr);
String line=null;
while( (line=in.readLine())!=null )
{
if(len != 0)  // 处理换行符的问题
{
str.append("\r\n"+line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str.toString();
}

--------------------编程问答-------------------- 各有各的用处,字符流是按char来读取的,某些文件就不能这样读。
而字节流是按照byte方式来读取的,所有文件都可以使用。
至于效率,其实都差不多。你都可以使用byte[]或者char[]来读呗,又不一定非要一个个的读,那当然慢了 --------------------编程问答-------------------- 大神们发表发表看法啊 --------------------编程问答-------------------- 各有各的好处 --------------------编程问答-------------------- ╮(╯▽╰)╭
这算是回答么,我还可以说各有各的缺点呢。。。
求详解。。。

引用 3 楼 acesidonu 的回复:
各有各的好处
--------------------编程问答-------------------- 求详解~~~~ --------------------编程问答-------------------- 如果你单纯追求把信息从文件中读取出来的速度的话,必然是二进制流,类似FileInputStream速度最快,因为根本不考虑任何转换,有啥读取啥。

而Reader类的,你要读取字符或字符串出来,必然需要进行编码转换工作,必然需要额外点点的时间。


至于你是用InputStream读取出byte[]之后,再自行new String(),还是直接用Reader来readLine()之类的直接得到String,其实基本没啥区别。如果你去看源码的话,其实大多数Reader的实现就是用StringBuffer在组装字符串。



但如果你自己直接用InputStream读取出byte[],再组装String(),可能面临所读取字节无法转换为字符的问题。比如汉字的UTF-8编码是3个字节,结果你刚好只读取出了1个byte,那么肯定new String()就出乱码。 --------------------编程问答-------------------- 顺便说下,你的方法二,歪曲了FileReader的标准用法,应该是:

char[] cbuf = new char[8192];
fr.read(cbuf, 0, cbuf.length)  --------------------编程问答-------------------- 多谢指点~~

引用 7 楼  的回复:
顺便说下,你的方法二,歪曲了FileReader的标准用法,应该是:

char[] cbuf = new char[8192];
fr.read(cbuf, 0, cbuf.length)
--------------------编程问答-------------------- 居然还没结贴~~~ --------------------编程问答-------------------- 搞一个大点的文件试试噜,执行之前记一下当前的时间,结束的时候打印一下时间,看看哪个短就效率高。。不过我一般都是用第一种方法。。 --------------------编程问答-------------------- 理论上应该是带buffer的好  也就是用BufferedInputStream
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,