-1

I have a text file of 5631 lines. I want to extract and store the number which appears after the word bytes at

Sample line form middle of the file is below :

debug3: In write loop, ack for 1606 32768 bytes at 52527104

Here I want the value 52527104. Each line this value increases and my goal is to calculate the percent completion based on each lines value. By using the formula ((obtained*100)/total).

Thanks.

akash
  • 22,664
  • 11
  • 59
  • 87
S. Das
  • 75
  • 3
  • 9

2 Answers2

2

You can simply split the read lines by bytes at and use the value at index 1. I have used Long parser, you can use Integer as per your requirement. The code will ignore lines, which doesn't contain the pattern provided.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BytesAt {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner read = new Scanner(new File("input.txt"));
    while (read.hasNextLine()) {
      String line = read.nextLine();
      String parts[] = line.split("bytes at");
      if(parts.length > 1) {
        System.out.println("Value as String: " + parts[1].trim());
        System.out.println("Value as Long: " + Long.parseLong(parts[1].trim()));
      }
    }
  }
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
0

How about:

long totalBytes = //Whatever source.
long currValue = 0;
for (String line: lines) {
  if (line.contains("bytes at")) {
    long currValue = Long.parseLong(line.split("bytes at\\s")[1]);
  }
  System.out.println("Completed % = "+(float)(currValue/totalBytes*100);
}
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36