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

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

如下代码:
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();
}
}
--------------------编程问答-------------------- 需要读取一行时,设计到ascii码判断,所以建议不要使用NIO,使用io包里的BufferedReader中的readLine方法读取 --------------------编程问答--------------------
引用 1 楼 gufengwyx1 的回复:
需要读取一行时,设计到ascii码判断,所以建议不要使用NIO,使用io包里的BufferedReader中的readLine方法读取

NIO不能一行一行 readline吗?
--------------------编程问答-------------------- 这个是我目前测试的最快读取文本文件的方式了。不过没法看啊
不像bufferreader那样,readline很方便
--------------------编程问答-------------------- A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. 

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

public void getFile2(){
try {
File file = new File("E:\\a.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("E:\\b.txt")));

String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
bw.write(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
--------------------编程问答-------------------- 楼主用的方法不常用。不评论,发一种常用的方法,可以一行一行的读取。

package com.briup.ch11;
//第一个
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;

public class FileInputOutputTest {
public static void main(String[] args) {
//声明流,
FileInputStream fis=null;
//包装流
BufferedInputStream bis=null;

FileOutputStream fos=null;
BufferedOutputStream bos=null;
//初始化并且包装流
try {
//要存储的源文件
fis=new FileInputStream("src/com/briup/ch11/FileInputOutputTest.java");
bis = new BufferedInputStream(fis);
//要保存的文件的位置
fos=new FileOutputStream("src/com/briup/ch11/FileInputOutputTest.txt");
bos = new BufferedOutputStream(fos);
//输入流读取文件
byte[] buff=new byte[128];
int len=0;
while((len=bis.read(buff))!=-1){
System.out.println(new String(buff,0,len));
bos.write(buff,0,len);
}
//输出流需要刷新缓冲区
bos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭释放流
if(bis!=null)bis.close();
if(fis!=null)bis.close();
if(bos!=null)bos.close();
if(fos!=null)fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}

--------------------编程问答-------------------- 直接看java一下BufferedReader中的readLine方法源代码,自己分析一下,就OK了。 --------------------编程问答-------------------- 另外FileInputStream这个流貌似不提供定位的功能。
分批读取的意思是一次读取一部分,这样多线程读取大数据量的信息是么?
如果那样的需要用到
PipedInputStream
PipedOutputStream
管道流,记得还有一个是可以定位读取的流,临时忘了,帮你查查API吧
--------------------编程问答--------------------
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());
--------------------编程问答--------------------
引用 5 楼 AA5279AA 的回复:
楼主用的方法不常用。不评论,发一种常用的方法,可以一行一行的读取。


Java code??



1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

package com.briup.ch11; //第一个……

谢谢,第一种方法我测试过,读50M的文本文件大约1.2秒
第二种没用过,更快? --------------------编程问答--------------------
引用 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……

谢谢,这个我看不懂的 --------------------编程问答--------------------
引用 7 楼 AA5279AA 的回复:
另外FileInputStream这个流貌似不提供定位的功能。
分批读取的意思是一次读取一部分,这样多线程读取大数据量的信息是么?
如果那样的需要用到
PipedInputStream
PipedOutputStream
管道流,记得还有一个是可以定位读取的流,临时忘了,帮你查查API吧

是得,不过没打算多线程同时读一个文件,因为是单硬盘
只想1个线程读文件,然后每读10万行,把这10万行内容分发给别的线程处理

比如3个线程好了,一个线程负责读,另外两个线程负责处理
--------------------编程问答--------------------
引用 6 楼 caoniaozhilu 的回复:
直接看java一下BufferedReader中的readLine方法源代码,自己分析一下,就OK了。

同意 --------------------编程问答--------------------
引用 6 楼 caoniaozhilu 的回复:
直接看java一下BufferedReader中的readLine方法源代码,自己分析一下,就OK了。

怎么个OK了? --------------------编程问答--------------------
引用 5 楼 AA5279AA 的回复:
楼主用的方法不常用。不评论,发一种常用的方法,可以一行一行的读取。


Java code??



1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

package com.briup.ch11; //第一个……

谢谢,测试了你的第二种方法,很慢,比bufferreader慢3.5倍 --------------------编程问答--------------------

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包装一下就行了,
不过文件大了受不了,
你可以考虑每次读定长,
然后再往后一直读到换行符为止 --------------------编程问答--------------------
引用 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吧 --------------------编程问答--------------------
引用 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也包装进去?
谢谢 --------------------编程问答-------------------- 什么叫做比BufferedReader慢,
你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的,
BufferedReader本身是跟Stream什么都没有关系的,
它只负责操作你给它的数据,
你给它数据的时候慢,当然它给你结果就慢
你把一个内存里面的字符串直接让BufferedReader读,那还会觉得慢? --------------------编程问答--------------------
引用 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"
--------------------编程问答--------------------
引用 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;)
       {
       }
--------------------编程问答--------------------
引用 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);
}
}

你是要这样? --------------------编程问答--------------------
引用 20 楼 wannaspring 的回复:
引用 18 楼 wapigzhu 的回复:什么叫做比BufferedReader慢,
你说的慢是因为平时用的时候,它的底层数据是从FileInputStream里面来的,
BufferedReader本身是跟Stream什么都没有关系的,
它只负责操作你给它的数据,
你给它数据的时候慢,当然它给你结果就慢
你把一个内存里面的字符串直接让BufferedReade……

当然用nio的更快咯,他是一整块内存直接读的
new FileReader("test.txt"))这个是按stream读的
底层操作会比nio的多很多 --------------------编程问答--------------------
引用 19 楼 wannaspring 的回复:
引用 15 楼 wapigzhu 的回复:Java code??



12345678910111213

public static void main(String[] args) throws IOException {         RandomAccessFile file = new RandomAccessFile("1.txt", "r")……

indexOf不能满足这个需求么? --------------------编程问答--------------------
引用 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)
       {
       }
--------------------编程问答--------------------
引用 23 楼 wapigzhu 的回复:
引用 19 楼 wannaspring 的回复:
引用 15 楼 wapigzhu 的回复:Java code??



12345678910111213

public static void main(String[] args) throws IOException {         RandomAccessFile file = new RandomAccessFile(……


不只是判断一个字符,有好几个,如果正则是很慢的,不考虑
但是如果 indexOf N次,就会遍历字符串N次,也慢 --------------------编程问答--------------------
引用 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);
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,