Java大文件读取

1.内存读取

import com.google.common.io.Files;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class Reading {


    public static void main(String[] args) {

        try {
            List lineOfApacheIO = FileUtils.readLines(new File("File Path"));

            List linesOfIoUtils = IOUtils.readLines(new FileInputStream(new File("File Path")), Charsets.UTF_8);

            List lineOfGuava = Files.readLines(new File("File Path"), Charsets.UTF_8);
        } catch (Exception e){

        }

    }
}

内存占用较大

2. 流式读取

        try (FileInputStream inputStream = new FileInputStream("File Path");
             Scanner sc = new Scanner(inputStream, "UTF-8")){
            while (sc.hasNextLine()){
                String line = sc.nextLine();
            }
            
            if (sc.ioException() != null){
                //occur error when scan
                throw sc.ioException();
            }
        } catch (FileNotFoundException e){
            log.info("file is not found");
        } catch (IOException e){
            log.info("file read occur error");
        } catch (Exception e){
            log.info("occur error");
        }

内存占用小

3.第三方流式读取

import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

	public static void main(String[] args){
        LineIterator lines = IOUtils.lineIterator(new FileInputStream("File Path"), Charsets.UTF_8);
        while (lines.hasNext()){
            lines.nextLine();
        }
    }

同上

你可能感兴趣的:(java)