Results 1 to 6 of 6
  1. #1
    smitho is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Can you open ByteArrayOutputStream without creating file first.

    In the example below the file hello_memory.pdf is created and then opened. Is it possible to open the pdf without creating the file first? If so how?

    Java Code:
    package org.HelloWorld;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.PdfWriter;
     
    /**
     * Creates a PDF file in memory.
     */
    public class HelloWorldMemory {
     
        /** Path to the resulting PDF file. */
        public static final String RESULT = "hello_memory.pdf";
     
        /**
         * Creates a PDF file: hello_memory.pdf
         * @param    args    no arguments needed
         * @throws InterruptedException 
         */
        public static void main(String[] args)
            throws DocumentException, IOException, InterruptedException {
            // step 1
            Document document = new Document();
            // step 2
            // we'll create the file in memory
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello World!"));
            // step 5
            document.close();
     
            // let's write the file in memory to a file anyway
            FileOutputStream fos = new FileOutputStream(RESULT);
            fos.write(baos.toByteArray());
            fos.close();
            
            if ((new File("hello_memory.pdf")).exists()) {
            	 
    			Process p = Runtime
    			   .getRuntime()
    			   .exec("rundll32 url.dll,FileProtocolHandler hello_memory.pdf");
    			p.waitFor();
     
    		} else {
     
    			System.out.println("File is not exists");
     
    		}
    
            
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Can you open ByteArrayOutputStream without creating file first.

    Huh?

    How can you open a file if it doesn't exist?

  3. #3
    smitho is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Can you open ByteArrayOutputStream without creating file first.

    Sorry Junky,

    What I'd like to do is open the file without creating the hello_memory.pdf on the hard drive.

    I would like to do something like

    Java Code:
    public class HelloWorldMemory {
    
        /**
         * Creates a PDF file: hello_memory.pdf
         * @param    args    no arguments needed
         * @throws InterruptedException 
         */
        public static void main(String[] args)
            throws DocumentException, IOException, InterruptedException {
            // step 1
            Document document = new Document();
            // step 2
            // we'll create the file in memory
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello World!"));
            // step 5
            document.close();
            	 
    			Process p = Runtime
    			   .getRuntime()
    			   .exec("rundll32 url.dll,FileProtocolHandler "+baos.toByteArray() );
    			p.waitFor();
             
        }
    }
    but nothing happens

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Can you open ByteArrayOutputStream without creating file first.

    Yeah and my question still stands, how do you expect to open a file if it doesn't exist? If all you want to do is display some text then write your own GUI and display it using a JTextArea. If you want to display it using Adobe Acrobat Reader or some other program then you have to create the file.

  5. #5
    smitho is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Can you open ByteArrayOutputStream without creating file first.

    Thanks Junky,

    I was hoping that I might be able to open the file in Acrobat reader for example without creating the file similar to how you can create a pdf on a web-server and open it without creating the file to disk just in memory. Much appreciated for the info.

    Cheers.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Can you open ByteArrayOutputStream without creating file first.

    open the file in Acrobat reader for example without creating the file
    You could if the app had an interface that took input from a java class vs from a disk file.
    Not many programs have that. Most require the data to be read be in a disk file.

    A useful solution here would be the ability to create a virtual drive (in memory) that you could write the file to and that the OS would support as a drive and that you could pass to the app.

Similar Threads

  1. Display sound data value from ByteArrayOutputStream
    By cielle in forum Advanced Java
    Replies: 2
    Last Post: 12-14-2010, 08:47 AM
  2. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  3. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  4. ByteArrayInputStream and ByteArrayOutputStream
    By new_2_java in forum New To Java
    Replies: 6
    Last Post: 07-31-2008, 10:02 PM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •