请教怎样从文件读到的内容一行行分析

wannaspring 2012-12-01 03:59:51
如下代码:
1、怎样把读到的内容(在BUFFER里)一行一行进行分析? 也就是 getline操作
2、如下的读法,可以只读一部分文件的内容吗?也就是分批读取?
谢谢

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class test
{

public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream = new FileInputStream("tconfirmdetail.txt");
FileChannel inChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(57972956);
while(true)
{
int eof = inChannel.read(byteBuffer);
if(eof == -1 ) break;
byteBuffer.flip();
byteBuffer.clear();
}
inChannel.close();
}
}
...全文
540 27 打赏 收藏 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
SkyJS 2014-10-24
  • 打赏
  • 举报
回复
NIO:是文件映射内存的方式,可以方便你处理大文件。不用担心由于文件过大一次读到内存中造成内存溢出 BufferedReader ,这个方式应该比没有缓冲区的读速度快。因为他是从文件中分批读取内容的。 传统的没有缓冲区的文件读取速度最慢。 如果是小文件很显然BufferedReader要更快,但是大文件我想目前只能用NIO去处理了。
wapigzhu 2012-12-02
  • 打赏
  • 举报
回复
引用 24 楼 wannaspring 的回复:
引用 22 楼 wapigzhu 的回复:引用 20 楼 wannaspring 的回复: 引用 18 楼 wapigzhu 的回复:什么叫做比BufferedReader慢, 你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的, BufferedReader本身是跟Stream什么都没有关系的, 它只负责操作你给它的数据, 你给……
你是怎么测的? 我刚试过了,一个11M的小说txt文件, 读出来转码并存到ArrayList, 用nio只用50ms左右 用FileReader花了235ms 代码在下面你可以试试看? nio的

		RandomAccessFile file = new RandomAccessFile("b.txt", "r");
		FileChannel fileChannel = file.getChannel();
		ByteBuffer buffer = ByteBuffer.allocateDirect((int) fileChannel.size());
		fileChannel.read(buffer);
		buffer.flip();
		CharBuffer charBuffer = Charset.forName("utf-8").decode(buffer);
		file.close();
		BufferedReader bufferedReader = new BufferedReader(new StringReader(charBuffer.toString()));
		List<String> list = new ArrayList<String>();
		long time = System.currentTimeMillis();
		String str = null;
		while((str = bufferedReader.readLine()) != null){
			list.add(str);
		}
		System.out.println(System.currentTimeMillis() - time);
FileReader

BufferedReader bufferedReader = new BufferedReader(new FileReader("b.txt"));
		String str = null;
		long time = System.currentTimeMillis();
		List<String> list = new ArrayList<String>();
		while((str = bufferedReader.readLine()) != null){
			list.add(str);
		}
		System.out.println(System.currentTimeMillis() - time);
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 23 楼 wapigzhu 的回复:
引用 19 楼 wannaspring 的回复: 引用 15 楼 wapigzhu 的回复:Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile(……
不只是判断一个字符,有好几个,如果正则是很慢的,不考虑 但是如果 indexOf N次,就会遍历字符串N次,也慢
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 22 楼 wapigzhu 的回复:
引用 20 楼 wannaspring 的回复: 引用 18 楼 wapigzhu 的回复:什么叫做比BufferedReader慢, 你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的, BufferedReader本身是跟Stream什么都没有关系的, 它只负责操作你给它的数据, 你给它数据的时候慢,当然它给你结果就慢 你把一个内存里面的字符串直接让……
我已经糊涂了,NIO,DIRECT BUFFER(ALLOCATEDIRET) CHANNEL 简单说吧,如下代码是我目前知道的最快的方式了,是否有更快的? NIO结合channel我测试了,比下面的快13%,但是是读到buffer,然后从buffer解码,再读成一行一行,貌似反而更慢了 BufferedReader reader = new BufferedReader(new FileReader("test.txt")); reader.readLine(); String line = null; String item[]; while ((line = reader.readLine()) != null) { }
wapigzhu 2012-12-01
  • 打赏
  • 举报
回复
引用 19 楼 wannaspring 的回复:
引用 15 楼 wapigzhu 的回复:Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.txt", "r")……
indexOf不能满足这个需求么?
wapigzhu 2012-12-01
  • 打赏
  • 举报
回复
引用 20 楼 wannaspring 的回复:
引用 18 楼 wapigzhu 的回复:什么叫做比BufferedReader慢, 你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的, BufferedReader本身是跟Stream什么都没有关系的, 它只负责操作你给它的数据, 你给它数据的时候慢,当然它给你结果就慢 你把一个内存里面的字符串直接让BufferedReade……
当然用nio的更快咯,他是一整块内存直接读的 new FileReader("test.txt"))这个是按stream读的 底层操作会比nio的多很多
wapigzhu 2012-12-01
  • 打赏
  • 举报
回复
引用 17 楼 wannaspring 的回复:
引用 15 楼 wapigzhu 的回复:Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.txt", "r")……

public static void main(String[] args) throws IOException {
		RandomAccessFile file = new RandomAccessFile("1.txt", "r");
		FileChannel fileChannel = file.getChannel();
		//这个地方要按文件大小来,如果实在要定长就只能按先前说的,继续往后读到换行符
		//或者把已经读过但是没有凑成正行的put回去,然后compact,再继续读
		ByteBuffer buffer = ByteBuffer.allocateDirect((int) fileChannel.size());
		fileChannel.read(buffer);
		buffer.flip();
		CharBuffer charBuffer = Charset.forName("utf-8").decode(buffer);
		file.close();
		BufferedReader bufferedReader = new BufferedReader(new StringReader(charBuffer.toString()));
		String str = null;
		while((str = bufferedReader.readLine()) != null){
			System.out.println(str);
		}
	}
你是要这样?
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 18 楼 wapigzhu 的回复:
什么叫做比BufferedReader慢, 你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的, BufferedReader本身是跟Stream什么都没有关系的, 它只负责操作你给它的数据, 你给它数据的时候慢,当然它给你结果就慢 你把一个内存里面的字符串直接让BufferedReader读,那还会觉得慢?
谢谢,我的意思是和下面代码比,哪个更快? BufferedReader reader = new BufferedReader(new FileReader("test.txt")); reader.readLine(); String line = null; String item[]; for (String line = null; (line = reader.readLine()) != null;) { }
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 15 楼 wapigzhu 的回复:
Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.txt", "r"); FileChannel fileChannel ……
另外想再请教一下,readline 后 怎样以最快的方式判断这一行是否含有比如:“你”,“我”等字符串? 特征是“你”“我”等字符串总是出现在行的比较靠右边的位置,比如 "8457TRGDF","FSAFDFFDF","FSFKJDHFSF","哦你好啊","AA"
wapigzhu 2012-12-01
  • 打赏
  • 举报
回复
什么叫做比BufferedReader慢, 你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的, BufferedReader本身是跟Stream什么都没有关系的, 它只负责操作你给它的数据, 你给它数据的时候慢,当然它给你结果就慢 你把一个内存里面的字符串直接让BufferedReader读,那还会觉得慢?
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 15 楼 wapigzhu 的回复:
Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.txt", "r"); FileChannel fileChannel ……
谢谢,能不能把那个allocatedirect 也就是 direct buffer也包装进去? 谢谢
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 15 楼 wapigzhu 的回复:
Java code?? 12345678910111213 public static void main(String[] args) throws IOException { RandomAccessFile file = new RandomAccessFile("1.txt", "r"); FileChannel fileChannel ……
这样能比BUFFERRADER快吗?如果不能快,就没意思了 文件有1.2G吧
wapigzhu 2012-12-01
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
		RandomAccessFile file = new RandomAccessFile("1.txt", "r");
		FileChannel fileChannel = file.getChannel();
		ByteBuffer buffer = fileChannel.map(MapMode.READ_ONLY, 0, fileChannel.size());
		CharBuffer charBuffer = Charset.forName("utf-8").decode(buffer);
		file.close();
		BufferedReader bufferedReader = new BufferedReader(new StringReader(charBuffer.toString()));
		String str = null;
		while((str = bufferedReader.readLine()) != null){
			System.out.println(str);
		}
	}
用BufferedReader包装一下就行了, 不过文件大了受不了, 你可以考虑每次读定长, 然后再往后一直读到换行符为止
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 5 楼 AA5279AA 的回复:
楼主用的方法不常用。不评论,发一种常用的方法,可以一行一行的读取。 Java code?? 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 package com.briup.ch11; //第一个……
谢谢,测试了你的第二种方法,很慢,比bufferreader慢3.5倍
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 6 楼 caoniaozhilu 的回复:
直接看java一下BufferedReader中的readLine方法源代码,自己分析一下,就OK了。
怎么个OK了?
平凡_幸福 2012-12-01
  • 打赏
  • 举报
回复
引用 6 楼 caoniaozhilu 的回复:
直接看java一下BufferedReader中的readLine方法源代码,自己分析一下,就OK了。
同意
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 7 楼 AA5279AA 的回复:
另外FileInputStream这个流貌似不提供定位的功能。 分批读取的意思是一次读取一部分,这样多线程读取大数据量的信息是么? 如果那样的需要用到 PipedInputStream PipedOutputStream 管道流,记得还有一个是可以定位读取的流,临时忘了,帮你查查API吧
是得,不过没打算多线程同时读一个文件,因为是单硬盘 只想1个线程读文件,然后每读10万行,把这10万行内容分发给别的线程处理 比如3个线程好了,一个线程负责读,另外两个线程负责处理
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 8 楼 huntor 的回复:
Java code?? 12345 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.Charset; List<String> lines = Files.readAllLines(Paths.get("D:/Temp", "examples.txt"), Cha……
谢谢,这个我看不懂的
wannaspring 2012-12-01
  • 打赏
  • 举报
回复
引用 5 楼 AA5279AA 的回复:
楼主用的方法不常用。不评论,发一种常用的方法,可以一行一行的读取。 Java code?? 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 package com.briup.ch11; //第一个……
谢谢,第一种方法我测试过,读50M的文本文件大约1.2秒 第二种没用过,更快?
huntor 2012-12-01
  • 打赏
  • 举报
回复
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.Charset;

List<String> lines = Files.readAllLines(Paths.get("D:/Temp", "examples.txt"), Charset.defaultCharset());
加载更多回复(7)
代码下载链接: https://pan.quark.cn/s/a8fbca3925b4 《鸿蒙OS开发环境构建》指南系统性地阐述了配置和筹备鸿蒙OS开发所需的各种工具和条件的具体方法。鸿蒙OS,亦称HarmonyOS,是由华为研发的一款面向全场景的分布式操作系统,其目标是提供跨平台、多设备间无缝协作的使用体验。本指南涉及了从Linux服务器到Windows工作站的完整开发流程。指南中提及了MobaXterm,这是一款用于连接Linux源码服务器的软件,使得开发人员能够在Windows环境中远程访问Linux服务器。同时,HiTool作为烧录工具,用于将编译后的系统镜像写入开发板。IPOP.EXE则是一款串口终端软件,用于执行串行通信和调试任务。Embedded Studio用于开发设备驱动程序,而DevEco Studio是华为提供的图形化应用程序开发平台,支持C/C++语言,拥有代码编辑、编译、烧录和调试功能,被视为OpenHarmony智能设备开发者的首选集成开发环境。在硬件配置方面,指南列出了必需的设备,包括Linux服务器(推荐Ubuntu 16.04及以上版本),Windows工作台(兼容XP/7/10),以及Hi3518EV300 IoT Camera单板。开发期间,Windows工作台通过USB线与单板相连接,以实现数据传输。此外,为了开展开发工作,还需要安装putty、IPOP、tftp服务器等辅助软件,以及HiTool用于烧录操作。在软件系统要求方面,Linux服务器需要安装bash、Python3.7+、gn、ninja、LLVM等构建工具,这些工具对于生成和执行编译脚本具有关键作用。在Windows工作台上,建议采用Visual Studi...

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧