read a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • litun
    New Member
    • Mar 2008
    • 12

    read a file

    how can we read a .pdf file in java
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by litun
    how can we read a .pdf file in java
    like any other file. (see code below)

    But maybe you want tor rephrase your question, because you want to display it on screen in java, or convert it to different format, or copy it? I am just curious about your question, because you mention ".pdf" which has nothing to do with "reading" the file.

    here is how to read a file:
    Code:
    File sourceFile = new File(sourceFileName);
    
    if (! sourceFile.exists()) throw new IOException("[ERROR ]: no such source file: " + sourceFileName);
    if (! sourceFile.isFile()) throw new IOException("[ERROR]: can't copy directory: " + sourceFileName);
    if (! sourceFile.canRead()) throw new IOException("[ERROR]: source file is unreadable: " + sourceFileName);
    
    FileInputStream sourceStream = null;
    byte[] buffer = new byte[file.length()];
    sourceStream = new FileInputStream(sourceFile);
    
    int bytesRead = sourceStream.read(buffer);

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Originally posted by litun
      how can we read a .pdf file in java
      To do what? If you are just accessing it to re-name it, or move it the above post example will work. If you actually want to modify a PDF you will want to look at the iText dev library.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by litun
        how can we read a .pdf file in java
        Here's a suggestion that will help you, not only in this forum, or in learning Java, or even programming, but throughout your entire life: when you ask a question, don't assume people can read your mind. Explain your context. What is your goal? What are you trying to do and why?

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          hi

          have even tried to read a PDF file before you ask here.
          i think it should work normally. because you can read any type of file by java and its classes.
          you can read html, word documents, in,txt, and so on. why will it stop at PDF

          Good Luck
          hsn

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by hsn
            hi

            have even tried to read a PDF file before you ask here.
            i think it should work normally. because you can read any type of file by java and its classes.
            you can read html, word documents, in,txt, and so on. why will it stop at PDF

            Good Luck
            hsn
            Like has already been pointed out, the purpose is the most important thing. e.g Reading a file to display the text in it to the screen is done differently for different file formats.

            Comment

            Working...