Is there any way to write an array of images into file ( for example text file) and read them from file (in Java of course).
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Is there any way to write an array of images into file ( for example text file) and read them from file (in Java of course).
Yes. You can write images to a file, by concatenating one after the other. I think you'd need a record length field to keep track of each separate image.
Why do you want them in a text file? Images are composed of binary data that is useless to view in a text editor.
If you need to convert the bytes of the images to text, look at Base64 encoding.
I am making project and its GUI needs to show some images (that can be changed in program) and then I want to save them back. Saving images in folder "Images" and something like that will make things more difficult (difficulties with picture names etc...) I use the following program:
[highlight] BufferedImage image=ImageIO.read(new File(legal file path)); ByteArrayOutputStream baos=new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); byte[] bitii=baos.toByteArray(); FileOutputStream f=new FileOutputStream(new File(legal file path)); f.write(biti); [/highlight]
Yesterday it did not work properly, today it works norm can I use a hint of placing 1 or more empty lines between two different images and then read first one until I reach empty line and so on?
I would recommend against writing multiple images to the same file. Instead, create multiple files with one image per file.
To read/write images, see this post: Images (read/write and drawing)
What byte makes an empty line? The new line character? Since the image is binary, it can contain any byte value including the newline character. It could be most images would not have 3 newline characters in a row. But it is not impossible.
For an exercise: Write an image to a byte array. Create a histogram of the bytes used in an image by writing a loop to go thru that byte array and use the value of the byte to increment/count occurances. Then print out the results. For example:
byte[] theImage // the array with the image bytes int[] histogram = new int[256]; // the byte usage counters for(int i =0; i < theImage.length; i++) { histogram[theImage[i] & 0xFF]++; // count usage of this byte from the image } System.out.println("histo=" + Arrays.toString(histogram)); // show the counts
Last edited by Norm; August 29th, 2010 at 03:20 PM. Reason: Use int and add & 0xFF
Or use a ZipOutputStream and write the images as separate ZipEntries to a ZipFile.
db
Thanks for advices.
I have also found Base64 encoder and decoder at Encode/Decode to/from Base64 - Real's Java How-to and downloaded java mail from https://cds.sun.com/is-bin/INTERSHOP...-CDS_Developer
and how can I add this classes ( something like library) to be used in my project .
I tried to add this one to bootstrap entries (I am using Eclipse) but import of javax.mail.internet.MimeUtility is not recognized yet ?
Why do you want the image's bytes to be output to a text file? Is this a student exercise to see how to do something?
Sorry, I have no idea how to configure your IDE.
I am doing Java project in which each user should have its own picture that can be changed and I have to save these changes Since there can be many many users it can become a little bit difficult to deal with all these pictures and I wanted to write them in just one tex file but I have forsaken this idea (because it is a little bit complicated and I do not have too much time now to deal with all these conversions + javax.mail.internet.MimeUtility not recognized and so on) and will save each image separately in one folder.
You still haven't explained why you want to use a TEXT file. If that requirement is not part of some learning assignment, it's a downright stupid approach.
db
Darryl.Burke I think that you have not read my first post carefully I wrote : for example text file It can be any type of file but aim is to have just one file instead of many pictures. I am also sure that it will increase my project points and of course I want to learn how to do this (not just for project points).
How will the images be changed if they are all in one file? To change any one image in the file, you will have to write out the file with all of the images.user should have its own picture that can be changed and I have to save these changes
norm When program starts, it reads all of images from file Then a frame containing combo box for name selection is drawn. And if for example John Norm is selected then panel containing his picture and some infos is shown. We can change a picture that is shown and when we close application all these pictures should be saved back into file from which they were read.
Ok that could work if you read ALL of the images in at the start and write all of the images out at the end and don't do any I/O to the images file while the program is running.
I'm attaching a zip with a project I did a while back that does something like you are trying to do. I had a web site that restricted the number of files I could post. It did allow jar files and .dat files. So I put all the images in one file vs have separate files for each image. Enjoy.
Norm
Last edited by Norm; August 31st, 2010 at 10:44 AM.
Javabeginner (September 2nd, 2010)
Thanks for post.