Question (Java) is either very simple, or very complex (I cannot determine from my own experience)

It is necessary to count the number of lines in the file (any text format). If it helps with java and txt:

private static void getStringCount(File file) { int i=0; String line=null; BufferedReader bufferedReader = null; try{ FileReader fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); while(bufferedReader.readLine()!=null) i++; System.out.println("СТРОК - " + i); bufferedReader.close(); }catch(Exception e){} } 

then to pdf, doc, etc., the numbers are not the same.

Does anyone know how this can be implemented? Do you really need to write a handler for each format and connect the library?

  • 7
    pdf and doc are not text formats, but binary ones. Accordingly, you need to know the structure of the file. - a_gura

1 answer 1

I suspect, after all, I have to write something different for each type. Formats are very different, as I understand it. Although, maybe I can say nonsense, you can try to search for the end of line characters in byte files, but there may be a lot of false positives, such as in FB2, so it's not an option

  • five
    I suspect that at the beginning for different types of files you will still have to define what the "number of lines" is. For example, for the same pdf, not everything is so straightforward (for example, in pdf there are two pages with pictures with one square each. How many lines are there?) - KoVadim