File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes FileOutputStream is not writing in loop. Need help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FileOutputStream is not writing in loop. Need help." Watch "FileOutputStream is not writing in loop. Need help." New topic
Author

FileOutputStream is not writing in loop. Need help.

Frederik Ericsson
Ranch Hand

Joined: Apr 27, 2005
Posts: 128
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

Joined: Apr 27, 2005
Posts: 128
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();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileOutputStream is not writing in loop. Need help.
 
Similar Threads
Server Program - Responding To Post Requests From HTML pg.
JSF: Save session context to local file
How to send *.jpg using JMS?
Help me Plz.........Confused:
Unicode Char Problem