java读取txt文件 程序从txt中读取数据,txt文件较大,大概400W行。怎么读取最后10W行到一个数组中?要很快就能得到结果。谁能有一个比较好的方法?最好有代码给提示,io的readline肯定不行。请给别的提示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 除非这个文件头有特殊声明,否则是没有直接定位的方法但是如果你只是想读最后10W,那么从后往前读就可以了,用RandomAccessFile操作不过你不应该将10W行读入数组,这样非常可能导致内存溢出问题的。 400W行,然后要读最后10W行,还要读到一个数组里.还要很快得到结果你试试用windows自带的记事本打开,看你能多快打开? to 3楼: 能给点具体的代码看看吗?在Linux中可以用命令得到总行数的,怎么从后往前读呢? 读到数组是因为改动别人的程序,不想做很多的修改,程序决定的。。 10W行哦,My God,干啥不用数据库啊调用linux命令 tail -100000 XXXX > newfile然后读那个newfile去 如果不在linux中呢?直接在windows中怎么操作,貌似有人说用nio,可是俺不会。请大家给代码,谢谢。 每行不固定的,从尾部读取的话怎么读呢?我是菜鸟,大侠们能不能给个例子,谢谢格式如下:192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idx.css HTTP/1.1" 200 6681 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/main.css HTTP/1.1" 200 5846 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/channelBg.gif HTTP/1.1" 200 1709 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_01.gif HTTP/1.1" 200 1346 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_02.gif HTTP/1.1" 200 1337 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_03.gif HTTP/1.1" 200 1366 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_04.gif HTTP/1.1" 200 1360 TCP_MISS:DIRECT192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/Chinese/ HTTP/1.1" 200 83041 TCP_MISS:DIRECT 刚看了帖子,也是定位读取文件的行,不过你这个文件High大发了http://topic.csdn.net/u/20100608/14/dd144f29-5df5-41d2-be2e-420a330fd965.html?20636 写了个分线程读的,但是无法精确只读10W行看看行不,行的话,你就自己改改吧(很多小细节没写)把下面两个变量值改成MAX_LINE_COUNT = 10WMAX_LINE_COUNT = 400Wfinish函数里面随便写点你的东西package to.shin.sai;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.RandomAccessFile;public class Test4ReadLargeFile { private final long LAST_LINE_COUNT = 10; private final long MAX_LINE_COUNT = 400; private final int MAX_THREAD_COUNT = 5; private String[][] resArr = null; private boolean[] finished = null; private String fileEncode = ""; Object lock = new Object(); /** * @param args */ public static void main(String[] args) { String filePath = "c:\\largeFile.txt"; Test4ReadLargeFile trlf = new Test4ReadLargeFile(); trlf.read(filePath, "SJIS"); } public void read(String filePath, String charset) { long beginTime = System.currentTimeMillis(); System.out.println("开始时间:" + beginTime); this.fileEncode = charset; RandomAccessFile raf = null; try { raf = new RandomAccessFile(filePath, "r"); long fileSize = raf.length(); long beginPos = (MAX_LINE_COUNT - LAST_LINE_COUNT) * fileSize / MAX_LINE_COUNT; long length = fileSize - beginPos; long perLength = length / MAX_THREAD_COUNT; resArr = new String[MAX_THREAD_COUNT][]; finished = new boolean[MAX_THREAD_COUNT]; for (int i = 0; i < MAX_THREAD_COUNT; i++) { new ReadFileThread(beginPos + i * perLength, Math.min(beginPos + (i + 1) * perLength, fileSize - 1), filePath, i).start(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (null != raf) try { raf.close(); } catch (IOException ioe) { } } //等待结束 synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { } } long endTime = System.currentTimeMillis(); System.out.println("结束时间:" + endTime); System.out.println("耗时:" + (endTime - beginTime) + "ms"); } class ReadFileThread extends Thread { // the begin position private long beginPos = 0L; // the end position private long endPos = 0L; // the current position private long curPos = 0L; // the file private RandomAccessFile raf = null; private int resIndex = 0; public ReadFileThread(long beginPos, long endPos, String filePath, int resIndex) throws Exception { super("ReadFileThread: " + resIndex); if (endPos - beginPos > Integer.MAX_VALUE) throw new Exception("It's too large size to read for this job"); this.beginPos = beginPos; this.endPos = endPos; this.resIndex = resIndex; raf = new RandomAccessFile(filePath, "r"); raf.seek(beginPos); } private boolean checkRun() { try { // 起点和终点在同一行的话,不需要读 raf.seek(beginPos); raf.readLine(); long beginLine = raf.getFilePointer(); raf.seek(endPos); raf.readLine(); long endLine = raf.getFilePointer(); raf.seek(beginPos); if (beginLine == endLine) return false; } catch (Exception e) { e.printStackTrace(); } return true; } @Override public void run() { if (!checkRun()) return; try { // skip the first line raf.readLine(); curPos = raf.getFilePointer(); byte[] content = new byte[(int) (endPos - curPos + 1)]; int offset = 0; while (curPos + offset <= endPos) { offset += raf.read(content, offset, (int) (content.length - offset)); } byte[] res = content; // 读取最后一行 byte[] lastBytes = new byte[1024]; raf.read(lastBytes); int returnKeyIndex = 0; // 假设最后一行最大不超过1K,如果考虑全,还要写完整(我懒) for (int i = 0; i < lastBytes.length; i++) { if (lastBytes[i] == 10 || lastBytes[i] == 13) { returnKeyIndex = i; break; } } res = new byte[offset + returnKeyIndex]; System.arraycopy(content, 0, res, 0, offset); System.arraycopy(lastBytes, 0, res, offset, returnKeyIndex); String resStr = new String(res, fileEncode); resStr = resStr.replaceAll("\r", ""); resArr[resIndex] = resStr.split("\n"); noticeFinished(resIndex); } catch (Exception e) { e.printStackTrace(); } finally { if (null != raf) try { raf.close(); } catch (IOException ioe) { } } super.run(); } } private void noticeFinished(int index) { finished[index] = true; System.out.println("Thread " + index + " is over."); for (boolean f : finished) if (!f) return; synchronized (lock) { lock.notify(); } finish(); System.out.println("all data is readed"); } private void finish() { PrintWriter pw = null; try { pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("c:\\result.txt"), fileEncode)); for (int i = 0; i < resArr.length; i++) if (null != resArr[i]) for (int j = 0; j < resArr[i].length; j++) { pw.println(resArr[i][j]); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != pw) try { pw.close(); } catch (Exception ioe) { } } }} 求一个Java问题的思路 怎么能看到XXX.class 文件里的代码 推荐学习java的入门级书 JAVA中如何实现代码控制垃圾的立即回收? 为什么有些Thread.sleep()没被中断? 弱问:eclipse里怎么在多个文件内进行搜索? swing JComboBox 如何设置key显示 取值value 求解JAVA平台释意—————————— 请问c++和java可不可以一起学呀,谢谢 各位用JAVA写的应用程序如何打包成EXE文件与安装文件??? gui编程 java文件运行问题
能给点具体的代码看看吗?在Linux中可以用命令得到总行数的,怎么从后往前读呢? 读到数组是因为改动别人的程序,不想做很多的修改,程序决定的。。
然后读那个newfile去
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/main.css HTTP/1.1" 200 5846 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/channelBg.gif HTTP/1.1" 200 1709 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_01.gif HTTP/1.1" 200 1346 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_02.gif HTTP/1.1" 200 1337 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_03.gif HTTP/1.1" 200 1366 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/images/idxItemChannel_04.gif HTTP/1.1" 200 1360 TCP_MISS:DIRECT
192.168.1.98 - - [25/May/2010:18:34:09 +0800] "GET http://www.shhp.gov.cn/shhp_1/Chinese/ HTTP/1.1" 200 83041 TCP_MISS:DIRECT
http://topic.csdn.net/u/20100608/14/dd144f29-5df5-41d2-be2e-420a330fd965.html?20636
看看行不,行的话,你就自己改改吧(很多小细节没写)
把下面两个变量值改成
MAX_LINE_COUNT = 10W
MAX_LINE_COUNT = 400Wfinish函数里面随便写点你的东西
package to.shin.sai;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;public class Test4ReadLargeFile {
private final long LAST_LINE_COUNT = 10;
private final long MAX_LINE_COUNT = 400;
private final int MAX_THREAD_COUNT = 5;
private String[][] resArr = null;
private boolean[] finished = null;
private String fileEncode = "";
Object lock = new Object(); /**
* @param args
*/
public static void main(String[] args) {
String filePath = "c:\\largeFile.txt";
Test4ReadLargeFile trlf = new Test4ReadLargeFile();
trlf.read(filePath, "SJIS");
} public void read(String filePath, String charset) {
long beginTime = System.currentTimeMillis();
System.out.println("开始时间:" + beginTime);
this.fileEncode = charset;
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(filePath, "r");
long fileSize = raf.length();
long beginPos = (MAX_LINE_COUNT - LAST_LINE_COUNT) * fileSize / MAX_LINE_COUNT;
long length = fileSize - beginPos;
long perLength = length / MAX_THREAD_COUNT;
resArr = new String[MAX_THREAD_COUNT][];
finished = new boolean[MAX_THREAD_COUNT]; for (int i = 0; i < MAX_THREAD_COUNT; i++) {
new ReadFileThread(beginPos + i * perLength, Math.min(beginPos + (i + 1)
* perLength, fileSize - 1), filePath, i).start();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != raf)
try {
raf.close();
} catch (IOException ioe) {
}
} //等待结束
synchronized (lock) { try {
lock.wait();
} catch (InterruptedException e) {
}
} long endTime = System.currentTimeMillis();
System.out.println("结束时间:" + endTime);
System.out.println("耗时:" + (endTime - beginTime) + "ms");
} class ReadFileThread extends Thread {
// the begin position
private long beginPos = 0L;
// the end position
private long endPos = 0L;
// the current position
private long curPos = 0L;
// the file
private RandomAccessFile raf = null;
private int resIndex = 0; public ReadFileThread(long beginPos, long endPos, String filePath, int resIndex)
throws Exception {
super("ReadFileThread: " + resIndex);
if (endPos - beginPos > Integer.MAX_VALUE)
throw new Exception("It's too large size to read for this job");
this.beginPos = beginPos;
this.endPos = endPos;
this.resIndex = resIndex;
raf = new RandomAccessFile(filePath, "r");
raf.seek(beginPos);
} private boolean checkRun() {
try {
// 起点和终点在同一行的话,不需要读
raf.seek(beginPos);
raf.readLine();
long beginLine = raf.getFilePointer(); raf.seek(endPos);
raf.readLine();
long endLine = raf.getFilePointer(); raf.seek(beginPos); if (beginLine == endLine)
return false;
} catch (Exception e) {
e.printStackTrace();
} return true;
} @Override
public void run() {
if (!checkRun())
return;
try {
// skip the first line
raf.readLine();
curPos = raf.getFilePointer(); byte[] content = new byte[(int) (endPos - curPos + 1)];
int offset = 0;
while (curPos + offset <= endPos) {
offset += raf.read(content, offset, (int) (content.length - offset));
} byte[] res = content;
// 读取最后一行
byte[] lastBytes = new byte[1024];
raf.read(lastBytes);
int returnKeyIndex = 0;
// 假设最后一行最大不超过1K,如果考虑全,还要写完整(我懒)
for (int i = 0; i < lastBytes.length; i++) {
if (lastBytes[i] == 10 || lastBytes[i] == 13) {
returnKeyIndex = i;
break;
}
} res = new byte[offset + returnKeyIndex];
System.arraycopy(content, 0, res, 0, offset);
System.arraycopy(lastBytes, 0, res, offset, returnKeyIndex);
String resStr = new String(res, fileEncode);
resStr = resStr.replaceAll("\r", "");
resArr[resIndex] = resStr.split("\n");
noticeFinished(resIndex);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != raf)
try {
raf.close();
} catch (IOException ioe) {
}
} super.run();
}
} private void noticeFinished(int index) {
finished[index] = true;
System.out.println("Thread " + index + " is over.");
for (boolean f : finished)
if (!f)
return; synchronized (lock) {
lock.notify();
}
finish();
System.out.println("all data is readed");
} private void finish() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("c:\\result.txt"),
fileEncode));
for (int i = 0; i < resArr.length; i++)
if (null != resArr[i])
for (int j = 0; j < resArr[i].length; j++) {
pw.println(resArr[i][j]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != pw)
try {
pw.close();
} catch (Exception ioe) {
}
}
}
}