读取大文件,分割成每次读取5兆
public static void main(String[] args) throws IOException { //大文件读取 String filepath="C:\\Users\\doume\\Desktop\\spring cloud系列教程第一篇-介绍_files\\test1.txt"; File file = new File(filepath); BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file)); BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件 String line = ""; while((line = reader.readLine()) != null){ //TODO: write your business String name=reader.readLine(); System.out.println(name); } }
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
public class DZTZ {
public static void readFileByBytes(String fileName) {
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[200000000];
int byteread = 0;
in = new FileInputStream(fileName);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
int i=0;
while ((byteread = in.read(tempbytes)) != -1) {
String name="E:\\邮储\\文档\\河南项目\\新中评\\文档\\新乡存量房\\新乡存量房\\20220418\\"+i++ +"caoni.txt";
File file=new File(name);
if(!file.exists())
try{
file.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
try{
FileOutputStream out=new FileOutputStream(file);
out.write(tempbytes);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("结束");
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
public static void write(String path) {
File file=new File(path);
if(!file.exists())
try{
file.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
try{
FileOutputStream out=new FileOutputStream(file);
byte bt[]="i eat apple".getBytes();
out.write(bt);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
readFileByBytes("E:\\文档\\20220418\\btp-trace.log\\btp-trace.log");
}
}