user497087
user497087

Reputation: 1591

What's the most efficient way to process a large text file line by line

I (think) I'm processing a text file line by line until I find a specific token;

(Psuedo Code)

Scanner scanner = new Scanner(new FileReader("myTextFile.txt");
while (scanner.hasNext() {
    boolean found = process(scanner.nextLine();
    if (found) return;
}

Some of the files are huge. Does this code actually scan the file line by line or does either the Scanner or FileReader read the entire file into memory and then work it's way through the memory buffer line by line?

Upvotes: 1

Views: 2281

Answers (3)

Puce
Puce

Reputation: 38152

Path filePath = Paths.get("myTextFile.txt");
boolean found = false;
try (BufferedReader br = Files.newBufferedReader(filePath, CharSet.forName(<char-set-name>)){
   for (String line = br.readLine(); line != null; line = br.readLine()) {
     found = process(line);
     if (found){
        break;
     };
   }
}

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24336

You want BufferedInputStream

public static void main(String[] args) {

        File file = new File("C:\\testing.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
            fis = new FileInputStream(file);

            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);

            while (dis.available() != 0) {
                System.out.println(dis.readLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                bis.close();
                dis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

source

Upvotes: 1

PSR
PSR

Reputation: 40338

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean found = false;
while ((line = br.readLine()) != null) {
     if(line.equalsIgnoreCase("Your string"))
       found = true;
}

Upvotes: 3

Related Questions