Java를 사용하여 한 줄씩 큰 텍스트 파일을 읽는 방법은 무엇입니까?
Java를 사용하여 줄 단위로 약 5-6GB의 큰 텍스트 파일을 읽어야합니다.
어떻게 빨리 할 수 있습니까?
일반적인 패턴은
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
문자 인코딩이 없다고 가정하면 데이터를 더 빨리 읽을 수 있습니다. 예를 들어 ASCII-7이지만 큰 차이는 없습니다. 데이터로 수행하는 작업이 훨씬 더 오래 걸릴 가능성이 높습니다.
편집 : line
누출 범위를 피하는 덜 일반적인 패턴 입니다.
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
// process the line.
}
// line is not visible here.
}
업데이트 : Java 8에서는 할 수 있습니다.
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
참고 : #close 메서드가 호출되었는지 확인하기 위해 try-with-resource 블록에 Stream을 배치 해야합니다. 그렇지 않으면 GC가 훨씬 나중에 수행 할 때까지 기본 파일 핸들이 닫히지 않습니다.
이 블로그를보십시오 :
버퍼 크기를 지정하거나 기본 크기를 사용할 수 있습니다. 기본값은 대부분의 목적에 충분히 큽니다.
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
fstream.close();
java-8 이 출시 되면 (2014 년 3 월) 스트림을 사용할 수 있습니다.
try (Stream<String> lines = Files.lines(Paths.get(filename), Charset.defaultCharset())) {
lines.forEachOrdered(line -> process(line));
}
파일의 모든 행 인쇄 :
try (Stream<String> lines = Files.lines(file, Charset.defaultCharset())) {
lines.forEachOrdered(System.out::println);
}
다음은 전체 오류 처리 및 Java 7 이전의 charset 사양을 지원하는 샘플입니다. Java 7에서는 try-with-resources 구문을 사용하여 코드를 더 깔끔하게 만들 수 있습니다.
기본 문자 집합 만 원하면 InputStream을 건너 뛰고 FileReader를 사용할 수 있습니다.
InputStream ins = null; // raw byte-stream
Reader r = null; // cooked reader
BufferedReader br = null; // buffered for readLine()
try {
String s;
ins = new FileInputStream("textfile.txt");
r = new InputStreamReader(ins, "UTF-8"); // leave charset out for default
br = new BufferedReader(r);
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e)
{
System.err.println(e.getMessage()); // handle exception
}
finally {
if (br != null) { try { br.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (r != null) { try { r.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (ins != null) { try { ins.close(); } catch(Throwable t) { /* ensure close happens */ } }
}
다음은 전체 오류 처리 기능이있는 Groovy 버전입니다.
File f = new File("textfile.txt");
f.withReader("UTF-8") { br ->
br.eachLine { line ->
println line;
}
}
Java 8에서는 다음을 수행 할 수 있습니다.
try (Stream<String> lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable<String>) lines::iterator)
{
;
}
}
참고 사항 : Files.lines
(대부분의 스트림과 달리)에서 반환 한 스트림을 닫아야합니다. 이유가 여기에 언급 I는 사용하지 마십시오 forEach()
. 이상한 코드 (Iterable<String>) lines::iterator
는 Stream을 Iterable로 캐스팅합니다.
할 수있는 일은 스캐너를 사용하여 전체 텍스트를 스캔하고 텍스트를 한 줄씩 살펴 보는 것입니다. 물론 다음을 가져와야합니다.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public static void readText throws FileNotFoundException {
Scanner scan = new Scanner(new File("samplefilename.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
//Here you can manipulate the string the way you want
}
}
스캐너는 기본적으로 모든 텍스트를 스캔합니다. while 루프는 전체 텍스트를 탐색하는 데 사용됩니다.
이 .hasNextLine()
함수는 텍스트에 여전히 더 많은 줄이 있으면 true를 반환하는 부울입니다. 이 .nextLine()
함수는 전체 라인을 문자열로 제공하여 원하는 방식으로 사용할 수 있습니다. System.out.println(line)
텍스트를 인쇄 해보십시오 .
참고 : .txt는 파일 유형 텍스트입니다.
FileReader를 사용하면 인코딩 InputStreamReader
을 지정할 수 없으며 지정해야하는 경우 대신 사용합니다.
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
Windows에서이 파일을 가져온 경우 ANSI 인코딩 (Cp1252)이있을 수 있으므로 인코딩을 지정해야합니다.
Java 7 :
String folderPath = "C:/folderOfMyFile";
Path path = Paths.get(folderPath, "myFileName.csv"); //or any text file eg.: txt, bat, etc
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(path , charset)) {
while ((line = reader.readLine()) != null ) {
//separate all csv fields into string array
String[] lineVariables = line.split(",");
}
} catch (IOException e) {
System.err.println(e);
}
Java로 파일을 읽는 10 가지 방법을 문서화하고 테스트 한 다음 테스트 파일을 1KB에서 1GB까지 읽도록하여 서로에 대해 실행했습니다. 다음은 1GB 테스트 파일을 읽는 가장 빠른 3 가지 파일 읽기 방법입니다.
성능 테스트를 실행할 때 실제로 테스트 속도가 느려지므로 콘솔에 아무것도 출력하지 않았습니다. 그냥 읽기 속도를 테스트하고 싶었습니다.
1) java.nio.file.Files.readAllBytes ()
Java 7, 8, 9에서 테스트되었습니다. 이것은 전반적으로 가장 빠른 방법이었습니다. 1GB 파일 읽기는 일관되게 1 초 미만이었습니다.
import java.io..File;
import java.io.IOException;
import java.nio.file.Files;
public class ReadFile_Files_ReadAllBytes {
public static void main(String [] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
File file = new File(fileName);
byte [] fileBytes = Files.readAllBytes(file.toPath());
char singleChar;
for(byte b : fileBytes) {
singleChar = (char) b;
System.out.print(singleChar);
}
}
}
2) java.nio.file.Files.lines ()
이것은 Java 8 및 9에서 성공적으로 테스트되었지만 람다 식에 대한 지원이 부족하기 때문에 Java 7에서는 작동하지 않습니다. 1GB 파일을 읽는 데 약 3.5 초가 걸렸고, 더 큰 파일을 읽는만큼 2 위를 차지했습니다.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;
public class ReadFile_Files_Lines {
public static void main(String[] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
File file = new File(fileName);
try (Stream linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}
3) BufferedReader
Java 7, 8, 9에서 작동하도록 테스트되었습니다. 1GB 테스트 파일을 읽는 데 약 4.5 초가 걸렸습니다.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile_BufferedReader_ReadLine {
public static void main(String [] args) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
String line;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
}
여기에서 10 가지 파일 읽기 방법에 대한 전체 순위를 확인할 수 있습니다 .
Java 8에는 Files.lines()
. 입력 소스가 파일하지만 같은보다 추상적 인 것이 아닌 경우 Reader
또는 InputStream
, 당신은 할 수 스트림 비아 라인 BufferedReader
의의 lines()
방법.
예를 들면 :
try (BufferedReader reader = new BufferedReader(...)) {
reader.lines().foreach(line -> processLine(line));
}
에서 processLine()
읽은 각 입력 줄 을 호출 합니다 BufferedReader
.
대한 읽기 파일 자바 8
package com.java.java8;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* The Class ReadLargeFile.
*
* @author Ankit Sood Apr 20, 2017
*/
public class ReadLargeFile {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
try {
Stream<String> stream = Files.lines(Paths.get("C:\\Users\\System\\Desktop\\demoData.txt"));
stream.forEach(System.out::println);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
스캐너 클래스를 사용할 수 있습니다.
Scanner sc=new Scanner(file);
sc.nextLine();
에서 readLine()
방법 을 사용해야합니다 class BufferedReader
. 해당 클래스에서 새 개체를 만들고 그에 대해이 메서드를 작동하고 문자열에 저장합니다.
자바 -9 :
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
이를 달성하는 명확한 방법은
예를 들면 :
당신은 할 경우 dataFile.txt
현재 디렉토리에
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class readByLine
{
public readByLine() throws FileNotFoundException
{
Scanner linReader = new Scanner(new File("dataFile.txt"));
while (linReader.hasNext())
{
String line = linReader.nextLine();
System.out.println(line);
}
linReader.close();
}
public static void main(String args[]) throws FileNotFoundException
{
new readByLine();
}
}
BufferedReader br;
FileInputStream fin;
try {
fin = new FileInputStream(fileName);
br = new BufferedReader(new InputStreamReader(fin));
/*Path pathToFile = Paths.get(fileName);
br = Files.newBufferedReader(pathToFile,StandardCharsets.US_ASCII);*/
String line = br.readLine();
while (line != null) {
String[] attributes = line.split(",");
Movie movie = createMovie(attributes);
movies.add(movie);
line = br.readLine();
}
fin.close();
br.close();
} catch (FileNotFoundException e) {
System.out.println("Your Message");
} catch (IOException e) {
System.out.println("Your Message");
}
그것은 나를 위해 작동합니다. 희망도 당신을 도울 것입니다.
나는 보통 읽기 루틴을 간단하게합니다.
void readResource(InputStream source) throws IOException {
BufferedReader stream = null;
try {
stream = new BufferedReader(new InputStreamReader(source));
while (true) {
String line = stream.readLine();
if(line == null) {
break;
}
//process line
System.out.println(line)
}
} finally {
closeQuiet(stream);
}
}
static void closeQuiet(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignore) {
}
}
}
스트림을 사용하여보다 정확하게 수행 할 수 있습니다.
Files.lines(Paths.get("input.txt")).forEach(s -> stringBuffer.append(s);
다음을 사용할 수도 있습니다 apache commons io
.
File file = new File("/home/user/file.txt");
try {
List<String> lines = FileUtils.readLines(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
이 코드를 사용할 수 있습니다.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadTextFile {
public static void main(String[] args) throws IOException {
try {
File f = new File("src/com/data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null) {
System.out.println(readLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
org.apache.commons.io 패키지 를 사용하여 특히 Java 6 이하를 사용하는 레거시 코드에서 더 많은 성능을 제공했습니다.
Java7은 더 적은 예외 처리와 더 유용한 메소드로 더 나은 API를 제공합니다.
LineIterator lineIterator =null;
try{
lineIterator = FileUtils.lineIterator(new File("/home/username/m.log"), "windows-1256");//second parameter is optionanl
while (lineIterator.hasNext()){
String currentLine = lineIterator.next();
//some operation
}
}finally {
LineIterator.closeQuietly(lineIterator);
}
메이븐
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
참고 URL : https://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java
'programing tip' 카테고리의 다른 글
JSF, Servlet 및 JSP의 차이점은 무엇입니까? (0) | 2020.09.29 |
---|---|
“git merge -s ours”의“그들의”버전이 있습니까? (0) | 2020.09.29 |
JavaScript에서 선택적 함수 매개 변수를 수행하는 더 좋은 방법이 있습니까? (0) | 2020.09.29 |
Git에서 특정 커밋을 병합하는 방법 (0) | 2020.09.28 |
개인, 공용 및 보호 상속의 차이점 (0) | 2020.09.28 |