• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

FileOutputStream is not writing in loop. Need help.

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say if we have a document that has 4 pages. we are getting the Inputstream for every page. In the given code below , it gets the stream in loop for every page, but it is not writing it to the file more than once. I mean its writing only one page(1rst page). Is there anyway we can get 4 stream and write it to a image file? This coule be a great help if any one can give me working sample code.

ContentElementList docContentList = doc.get_ContentElements();
Iterator iter = docContentList.iterator();
while (iter.hasNext() )
{
ContentTransfer ct = (ContentTransfer) iter.next();
File f=new File("textfile1.txt");
FileOutputStream fop=new FileOutputStream(f);

// Print element sequence number and content type of the element
System.out.println("\nElement Sequence number: " +
ct.get_ElementSequenceNumber().intValue() + "\n" +
"Content type: " + ct.get_ContentType() + "\n");

// Get and print the content of the element
int docLen = ct.get_ContentSize().intValue();
byte[] buf = new byte[docLen];
InputStream stream = ct.accessContentStream(); //Everytime different contents are coming here, mean diff page everytime.
try
{
stream.read(buf, 0, docLen);
fop.write(buf); // here it writes only once.

}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
fop.flush();
fop.close();
 
Frederik Ericsson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry my mistake I got to define the FileOutputStream out of the loop. Still facing the same problem, please help me out.

File f=new File("textfile1.txt");
FileOutputStream fop=new FileOutputStream(f);

ContentElementList docContentList = doc.get_ContentElements();
Iterator iter = docContentList.iterator();
while (iter.hasNext() )
{
ContentTransfer ct = (ContentTransfer) iter.next();


// Print element sequence number and content type of the element
System.out.println("\nElement Sequence number: " +
ct.get_ElementSequenceNumber().intValue() + "\n" +
"Content type: " + ct.get_ContentType() + "\n");

// Get and print the content of the element
int docLen = ct.get_ContentSize().intValue();
byte[] buf = new byte[docLen];
InputStream stream = ct.accessContentStream(); //Everytime different contents are coming here, mean diff page everytime.
try
{
stream.read(buf, 0, docLen);
fop.write(buf); // here it writes only once.

}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
fop.flush();
fop.close();
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic