Solved

convert a outputstream

Posted on 2004-08-05
17
2,449 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
Comment Utility
why does getPDF return an output stream?

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

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
FileInputStream READS FROM a file, surely?

I may not be awake though (and missing the point) ;-)
0
 
LVL 35

Expert Comment

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

Expert Comment

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

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
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
Comment Utility
it really depends what the "getPDF" method actually does...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
yes. I agree that
0
 
LVL 3

Expert Comment

by:JohnnyAffa
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
:)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
wordappend challenge 8 83
Error with Java/Cache JDBC Classpath 2 19
eclipse formatting 6 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now