?
Solved

convert a outputstream

Posted on 2004-08-05
17
Medium Priority
?
2,462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 total points
ID: 11733271
0
 
LVL 35

Expert Comment

by:girionis
ID: 11889645
:)
0

Featured Post

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.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

764 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