Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jpg into ByteArray  RSS feed

 
Michael Uhlig
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I try to put a jpg into an ByteArray and write it back to a file. I need this because in the next step I want to decrypt and encrypt the file. I need a way to get the dates into the byteArray "streamArray" and back from the "streamArray" into a FileOutputStream. I�m new in Java. I hope somebody helps me.

try {
File file = new File(pathname +filename1);
int fileSize = (int) file.length();
byte[] streamArray = new byte[fileSize];
FileInputStream fileIn = new FileInputStream(file);
DataInputStream din = new DataInputStream (new BufferedInputStream(fileIn));

ByteArrayInputStream bI = new ByteArrayInputStream (streamArray);
ByteArrayOutputStream bA = new teArrayOutputStream();
streamArray = bA.toByteArray();

FileOutputStream fos1= new FileOutputStream ("bild.jpg");
fos1.write(streamArray);
[ June 19, 2005: Message edited by: Michael Uhlig ]
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at your code you can see that you never actually start reading you file.

You can use the "read" methods on inputstream for this.

This code should read and write the file. Notice that I do not do any looping for reading. If you read something from a network resource (or any resource that might not provide constant data feeding) you would most likely want to provide a loop that reads the different chunks of data. If you really want to be safe you might want to refine this code to do just that. You can do this using the read method and it's return value (an int indicating how much was read). Look at the javadoc for more information.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!