Solved

convert a outputstream

Posted on 2004-08-05
17
2,455 Views
Last Modified: 2012-05-05
Hi I have an outputstream that is returned from a method. I need to write it to a file.
Any help

public OutputStream getPDF()
{
....
}

public static void main()
{
File pdf = new File("c:\\my.pdf")

what do i do here so that i can write it to a file from getPDF method

}

Thx
0
Comment
Question by:applekanna
  • 7
  • 3
  • 3
  • +2
17 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11724487
why does getPDF return an output stream?

That doesn't sound right...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11724502
You need to have a FileInputStream instead of a File in order to create the file. Then use this FIS to write bytes into the declared file.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11724511
FileInputStream READS FROM a file, surely?

I may not be awake though (and missing the point) ;-)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:girionis
ID: 11724520
Eh,mm.. yes, he he, I meant FileOutputStream :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11724524
BTW I also agree with you, the method that returns an OutputStream seems weird.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11724817
As other experts said this is wiered method.

public OutputStream getPDF(File f) throws Exception
{
  return new FileOutputStream(f);
}


P.S : Just Curious.Are u writing codes using Multivalent or PdfBox??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11724872
May be u need some thing like this.

public OutputStream getPDF() throws Exception
{
    String test ="Hello World";
     ByteArrayOutputStream stream=new ByteArrayOutputStream();
         stream.write(test.getBytes());
        return stream;
}

public static void main()
{
               File pdf = new File("c:\\my.pdf")
               OutputStream stream=getPDF();
               FileOutputStream fout=new FileOutputStream();
               stream.writeTo(fout);
               stream.close();
               fout.close();    

}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11724883
it really depends what the "getPDF" method actually does...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11724926
yes. I agree that
0
 
LVL 3

Expert Comment

by:JohnnyAffa
ID: 11725099
apple, i, like everyone else is a little confused to say the least!

how does you method get an outputstream?

where is your data coming from that you want to write to a file?  

is it another file or is it text from a JTextArea or something similar?


to read in a file use

    File file = new File("C:\\my.pdf");

    if ( file.exists() == true )
    {
       if ( file.canRead() == true )
       {
           byte data[] = new byte[(int)file.length()];

           FileInputStream fis = new FileInputStream(new File("C:\\my.pdf"));

           while ( fis.read(data,0,data.length) != -1 );

// to convert file to string use String fileContents = new String(data);
// if you need to pass the file as a byte array simply pass the byte array data          
        }

to write to a file

File file = new File("C:\\output.txt"); // dont use output.pdf unless the data will be recognized by a pdf reader!

String data = "this is my data";

try
{
   FileOutputStream fos = new FileOutputStream(file);

   fos.write(data.getBytes());
   fos.close();
}
catch (Exception a)
{
   System.out.println(a.toString());
}
0
 
LVL 3

Author Comment

by:applekanna
ID: 11728256
Sorry for the delayin replying...

I am trying to generate a PDF usinf FOP/itext and make it avaiable for as a stream to be available to any form of output... that is y I choose to have an outputstream.... i see from the discussions that it does not sound right even though it might work... what would be the right thing to do.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11728370
Do you want to send it over the network somewhere? Then just open a socket, get the i/o streams and then send the bytes. On the other end read the data, open a file as suggested above (by using FileOutputSteam) and write the bytes there. If you give us more details we will be able to help you more.
0
 
LVL 3

Author Comment

by:applekanna
ID: 11728546
>network somewhere - NO

I am having 2 simple util files in my project  and need to generate the pdf in one file and send it to another and hold it as a stream for further processing

When i generate the pdf file, I generate it as a bufferedArrayOuptuStream so I thought I will return it as an outputstream from the method of one of the util files.

To test the outputstream, I wanted to get the stream and write it inot a file and c what happened

so

Class A
{
 getPDF OutputStream()
  {
    //generated a pdf and returns it as output stream
  }

 public static void main(String args[])
 {
   // i have the output stream
    OutputStream out = getPDF()
  //need to write it to  a file
 

 }

}



0
 
LVL 35

Expert Comment

by:girionis
ID: 11728752
What I would do would be to return the PDF object itself instead of returning the output stream of the PDF. It would be easier to manipulate. Then to save it to another file, you could do it by just saving the bytes but I am sure there will be some methods in the API you are using that help you do it (which one are you using?)

Class A
{
 public PDF getPDF()
  {
    //generated a pdf and returns it
  }

 public static void main(String args[])
 {
   // i have the PDF
   PDF pdf = getPDF();
  //I have a ByteArrayOutputStream called "baos" and I get a byte [] from it
  byte [] bytes = baos.toByteArray();
  FileOutputStream fos = new FileOutputStream("newpdf.pdf");
  fos.write(bytes);
  fos.flush();
  fos.close();
 

 }

}
0
 
LVL 3

Author Comment

by:applekanna
ID: 11730982
I am using the FOP package ....

Suppose I am to put in a database as a stream in a BLOB/CLOB how do I do that ?That is whay I wanted to leave it as a stream
0
 
LVL 35

Accepted Solution

by:
girionis earned 125 total points
ID: 11733271
0
 
LVL 35

Expert Comment

by:girionis
ID: 11889645
:)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
servlet example issue 6 46
How to convert String matching to regex in java 4 48
eclipse console opening separately 2 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question