package filereader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.Arrays;
public class FileReader {
public static int SIZE = 1024 * 1024 * 5;
public static String FILENAME = "homeuser.unl";
public static long bt;
/**
* @param args
*/
public static void main(String[] args) {
try {
bt = System.currentTimeMillis();
InputStream is = FileReader.class.getResourceAsStream(FILENAME);
readFile(is);
System.out.println(System.currentTimeMillis()-bt+":MS");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readFile(InputStream is) throws IOException {
byte[] buffer = new byte[SIZE];
StringBuffer sb = new StringBuffer();
int r = 0;
int i = 0;
while (is.available() > 0) {
i++;
String partContent = readPartFile(is, buffer, SIZE, sb);
sb = null;
//System.out.println(i + "\n" + partContent);
}
System.out.println("I: " + i);
}
public static String readPartFile(InputStream inputStream, byte[] buffer,
int size, StringBuffer sb) {
try {
InputStream is = inputStream;
int r = is.read(buffer);
if (r == size) {
byte[] other;
int count = 0;
byte n = (byte) is.read();
other = new byte[] { n };
sb.append(new String(buffer));
sb.append(new String(other));
while (n != 10 && n != -1) {
count++;
n = (byte) is.read();
other = new byte[] { n };
sb.append(new String(other));
}
if (count != 0) {
SIZE += count;
}
System.out.println("COUNT: " + count);
} else {
if (null == sb)
sb = new StringBuffer();
zjRead(r, sb, buffer);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static void zjRead(int r, StringBuffer sb, byte[] buffer) {
byte[] temp = new byte[r];
System.arraycopy(buffer, 0, temp, 0, r);
sb.append(new String(temp));
}
}
分享到:
相关推荐
linnux下可用标准C接口,分块读取文件,可编译 .so文件
用c++实现了多进程对文件的分块读取,并把读取的内容通过c++库函数对LINUX下的命令进行了系统调用
本文实例讲述了Python多进程分块读取超大文件的方法。分享给大家供大家参考,具体如下: 读取超大的文本文件,使用多进程分块读取,将每一块单独输出成文件 # -*- coding: GBK -*- import urlparse import datetime...
一个用C语言实现的分块读取文件程序,能有效的解决读取较大文件时间过久的问题
利用GDAL分块读取大图像,例子是计算一个栅格图像的NDVI值,采用分块读取的方法。
本文实例为大家分享了python多线程分块读取文件的具体代码,供大家参考,具体内容如下 # _*_coding:utf-8_*_ import time, threading, ConfigParser ''' Reader类,继承threading.Thread @__init__方法初始化 @run...
前端Javascript+Html5+后端PHP分块上传文件,PHP分块上传大文件,该项目可以正常运行,入口为index.html,需要正确配置fileDir的读写权限 目前有测试过上传1.5G左右的没有问题(未测试更大的文件),理论上更大的...
主要为大家详细介绍了C#利用缓存分块读写大文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
今天小编就为大家分享一篇Python 多线程不加锁分块读取文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
R语言读取大文件数据(分块读)
qt 内存 映射 大文件读写
面对1G以上的超大图片文件,直接加载会导致内存错误,这个代码片段就是用来读取图片文件图块来分块加载大图;
自动对S19文件不连续地址分块,内部数据校验,生成校验码,适合用于UDS协议刷写ECU前对S19文件的处理。
使用每行的字符个数统计和 MD5 两个算法比较内存映射和分块直接读取文件的速度。
该程序使用了Python的内置库,如os、sys等,实现了对文件的读取、写入和分块操作。用户只需输入源文件路径、目标文件夹路径以及分块大小,程序便会自动将源文件按照指定的分块大小拆分成多个小文件,并将这些小文件...