Solved

convert a outputstream

Posted on 2004-08-05
17
2,457 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
Running JavaFX on JDeveloper 12C 1 79
How to configure empty element in XML Document parser? 15 45
Netbeans and org.apache.commons.lang3 issue 3 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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