Solved

convert a outputstream

Posted on 2004-08-05
17
2,451 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
eclipse formatting 6 70
Need to track down Infection in a Server 2008 domain user profile 7 48
Java Jpanels and Jframe 8 28
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

930 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

8 Experts available now in Live!

Get 1:1 Help Now