查看大文件内容的方法

如果一个文件太大,例如一个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;
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值