查看大文件内容的方法
2014-07-26 22:58
225 查看
如果一个文件太大,例如一个txt文件好几个G这时候使用Notepad是打不开的。两种查看文件内容的方法
第一种,将大文件切分成小文件后再使用Notepad。
public class SplitFileByLine {
public static void main(String[] args) throws IOException {
String datasetPath = "bigFilePath"; 大文件路径
String decPath = ""; //切分后文件存放的路径
int N = 6000; //指定每个切分后文件的行数
int i = 0;
int j = 1;
String filePath = decPath + "name";//就是切分后文件的名字
File filename = new File(filePath + j);
String newline = System.lineSeparator();
File file = new File(datasetPath);
LineIterator it = FileUtils.lineIterator(file);
while(it.hasNext()) {
if(i>=N) {
j++;
filename = new File(filePath + j);
i = 0;
}
String data = it.next() + newline;
FileUtils.writeStringToFile(filename, data, true); //最好的方法是一次读取指定行数的内容,一次性存入文件,这样子需要每一行都需要硬盘IO一次。
i++;
}
System.out.println("finish.");
}
}
第二种:直接读取前几十行。
方法二代码:
public class ReadContentFromFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String path="这里是路径";//就是大文件的路径。
File file=new File(path);
FileReader read=new FileReader(file);
BufferedReader reader=new BufferedReader(read);
String temp;
int i=0;
while((temp=reader.readLine())!=null){
System.out.println(temp);
i++;
if(i>20)
break;
}
}
}
第一种,将大文件切分成小文件后再使用Notepad。
public class SplitFileByLine {
public static void main(String[] args) throws IOException {
String datasetPath = "bigFilePath"; 大文件路径
String decPath = ""; //切分后文件存放的路径
int N = 6000; //指定每个切分后文件的行数
int i = 0;
int j = 1;
String filePath = decPath + "name";//就是切分后文件的名字
File filename = new File(filePath + j);
String newline = System.lineSeparator();
File file = new File(datasetPath);
LineIterator it = FileUtils.lineIterator(file);
while(it.hasNext()) {
if(i>=N) {
j++;
filename = new File(filePath + j);
i = 0;
}
String data = it.next() + newline;
FileUtils.writeStringToFile(filename, data, true); //最好的方法是一次读取指定行数的内容,一次性存入文件,这样子需要每一行都需要硬盘IO一次。
i++;
}
System.out.println("finish.");
}
}
第二种:直接读取前几十行。
方法二代码:
public class ReadContentFromFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String path="这里是路径";//就是大文件的路径。
File file=new File(path);
FileReader read=new FileReader(file);
BufferedReader reader=new BufferedReader(read);
String temp;
int i=0;
while((temp=reader.readLine())!=null){
System.out.println(temp);
i++;
if(i>20)
break;
}
}
}
相关文章推荐
- 查看vsftp登录认证数据库db文件内容的方法
- Linux查看文件内容的方法(cat,tac,more,less,head,tail)
- 查看tar包文件内容的方法
- 某些CHM文件不能查看内容的解决方法
- pycham database查看db.sqlites文件 无内容解决方法
- 某些CHM文件不能查看内容的解决方法
- 一个完整的MSI包的配置文件XML的内容形式和查看方法ORCA
- 查看文件内容时,排除注释行查看的方法
- 选择性地查看文件的方法 head tail sed 查看文件部分内容
- 只查看ett.txt文件(共100行)内第25到35行的内容的八种解决方法
- Oracle数据库教程:查看控制文件内容的方法
- linux下查看文件第几行内容的方法
- 查看控制文件内容的方法
- linux下查看文件第20-30行内容的N种方法及命令介绍
- linux中tail命令---用于查看文件内容
- 追加内容到文件末尾的几种常用方法
- 用c#读取文件内容中文是乱码的解决方法
- VC读取写入 ini文件内容的方法函数详解
- 母版页跟内容页不在同一级目录导致文件错误解决方法
- [SQLServer]装SQl2000时,,遇到文件挂起,,须重启,,可以按以下方法解决----转 (查看:651 回复:14)