Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileOutputStream is not writing in loop. Need help.

 
Frederik Ericsson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
  • 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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic