HI
my package reads file from txt format only. it does not read .pdf or .doc file. is there any single java library that opens any kind of file format as stream and then reads or manipulates its contents.

Dani AI

Generated

Short answer: there is no single Java class that magically opens any file format and reliably returns plain text. That is the point and made: formats differ wildly (binary containers, layout-only formats, images). 's pipeline idea — detect, parse, then treat extracted text the same — is the practical approach.

Begin with a detector/extractor layer. Apache Tika provides a ready-made detector + extractor that delegates to format-specific parsers and returns plain text suitable for indexing or simple keyword counting. It is the easiest way to get a single entry point that handles hundreds of types. Apache Tika. (tika.apache.org)

Example (very small) extraction pattern using Tika:

import org.apache.tika.Tika;
import java.io.File;

Tika tika = new Tika();
try {
  String text = tika.parseToString(new File("somefile"));
  // normalize/tokenize 'text' and count keywords
} catch (Exception e) {
  // handle unreadable file / parse errors
}

For finer control use format-specific libraries: [Apache POI] handles MS Office (DOC/DOCX/XLS/XLSX); [Apache PDFBox] offers richer PDF manipulation; [jsoup] is the right tool to extract text from HTML. For scanned pages or image-only PDFs an OCR step (Tesseract or similar) is required before text extraction. For production indexing/searching use a library like [Apache Lucene] after extraction. (poi.apache.org)

Quick troubleshooting checklist:

  • Detect MIME/type by content (magic bytes) not just extension.
  • Normalize Unicode, strip control chars, and tokenize before counting.
  • Verify PDFs actually contain text (not images); if images, add OCR.
  • Test with representative files and measure extraction accuracy before indexing.

Recommended Answers

All 5 Replies

I doubt it. especially since .pdf is intended as a formatted output, not as input

HI
my package reads file from txt format only. it does not read .pdf or .doc file. is there any single java library that opens any kind of file format as stream and then reads or manipulates its contents.

The literal answer is yes: http://java.sun.com/javase/6/docs/api/java/io/package-summary.html

The actual answer to the question as you intend it:
No, and there never will be.
Any application could use anything it wanted for a file format and "manipulate its contents" depends completely on the nature of that application. You cannot expect to have a completely generic solution to an inherently non-generic problem.

HI
Thank you two for your reply. if what you are saying is true, then "Does it mean that there cannot be a java application that reads any kind of file and count the frequency of any given word in it."
1) i mean we can not use java for trying to make an index of keywords?
2) URL and URLConnection classes of java given me the html contents of the web page, is there any way to get just the content like text etc of the web page.

What it means is that you need identify exactly what you want to read and use appropriate APIs for that content. There are libraries for working with .doc files, .pdf files, etc., but there is not a "reading anything I might happen to come across" library because that is a completely unrealistic expectation.

HI
Thank you two for your reply. if what you are saying is true, then "Does it mean that there cannot be a java application that reads any kind of file and count the frequency of any given word in it."
1) i mean we can not use java for trying to make an index of keywords?
2) URL and URLConnection classes of java given me the html contents of the web page, is there any way to get just the content like text etc of the web page.

There can be a java application that reads any kind of file and count the frequency of any given word in it. BUT, when the application is given that "any" kind of file, that application has to recognize the type, and treat them differently while parsing data from the files. Once data is parsed, they can be treated as same. An application dealing with "any kind of file" and a class dealing with "any kind of file" is NOT the same.
1. I think I already said the answer. Precisely, you can.
2. So are you saying, you want the data from html file without the tags? Them parse the html file using DOM or SAX, and iterate through the nodes (the tags), and get whatever data you want. Or another way is to using regular expression to remove the tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.