「java文件的读效率」java读写文件效率

博主:adminadmin 2022-11-23 02:07:08 69

今天给各位分享java文件的读效率的知识,其中也会对java读写文件效率进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java io流如何读取文件效率高

你在类的使用上不太恰当。

如果你想读取文本内容,你应该使用InputStreamReader这个类,使用这个类并且指定文本内容的字符集,就可以读出正确的内容。

InputStream这个类是用来读取二进制字节的,比如做文件复制的时候,应该是用InputStream这个类。

不管是InputStreamReader还是InputStream,都可以声明临时缓冲数组,不同的是InputStreamReader声明的数组是:char[] cs = new char[1024]

而InputStream声明的数组是:byte[] bs = new byte[1024]

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

方式一

/**

* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

* 当然也是可以读字符串的。

*/

/* 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/

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();

}

JAVA读取文件和读取数据库的效率哪个高一些

顺序读取的话文件快。检索的话数据库高。

不只是java,所有语言都有这个特点。

在2000年左右,国内还争论过使用数据库还是文件来保存数据。现在已经没有争论了。数据存储就是为了用的。

java中如何提高读取文件内容的效率???

import java.util.*;

import java.io.*;

class Main{

static public void main(String[] str)throws Exception{

File f=new File("1.txt");

write(f,1024*1024);//写文件

read(f);

}//main

static public void write(File f,long length)throws Exception{

long begin=System.currentTimeMillis();

PrintWriter p=new PrintWriter(f);

for(long l=0;llength;l++){

char c=(char)(int)(Math.random()*70+40);

p.print(c);

}

long end=System.currentTimeMillis();

System.out.println("Write "+length+"char OK! Time:"+(end-begin));

}

static public void read(File f)throws Exception{

long begin=System.currentTimeMillis();

Scanner s=new Scanner(f);

while(s.hasNext()){

s.next();

}

long end=System.currentTimeMillis();

System.out.println("Read OK!Time:"+(end-begin));

}

}//class

java读大量csv文件竟然比c++快,正常吗

正常得很。大数据都基本都用jvm在做。 jvm对io做了很多优化, 比如加了缓冲区什么的。

读文件要快最主要的考虑就是尽量减少系统调用(read)的使用频率。 c++要是直接fopen后循环read, 那只会比python还慢。

另外java在多线程等很多内存/缓存瓶颈的操作都比c++快,原因还是jvm做了很多优化。 例举个例子, Jvm的变量在内存间会有一定间隔而不像c++仅仅只是对齐了下,这样可以防止多线程的cpu缓存伪共享。

但是上述所有功能都是建立在庞大的运行时上面的,内存消耗也是怪兽级别。 所以你觉得快,不见得真的效率高

如何提高java读取大文本文件的效率

JavaNIO的创建目的是为了让Java程序员可以实现高速I/O而无需编写自定义的本机代码。JavaNIO的高效得益于其两大"助手":Channel和Buffer。NIO将最耗时的I/O操作(即填充和提取缓冲区)转移回操作系统,因而可以极大地提高速度。

关于java文件的读效率和java读写文件效率的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。