Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

ImageIO.write () only writes once

 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I would like to describe a jpg with information from an array. I use graphics for that. The first time the method is called, the code creates the image with the information as desired. However, every further call does not change the target image. When I output the information on the console, I see that the correct information is arriving. I suspect ImageIO.write () is not releasing the file and I can't find a way to do it.

What can I do?!? Thank you!


menuBlanco

menuBlanco2


 
Tim Moores
Saloon Keeper
Posts: 6625
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

How are looking at the image? Make sure you close the image in the viewer before opening another one with the same name - changes would not necessarily be picked up automatically.

Have you tried writing the images to files with different names? Does that work?
 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already saved the files with numbered filenames. it works. I don't know how to end the file access, because there is no close () method
 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since, like you, I probably can't find a solution to my problem, I wanted to briefly ask for an alternative way. The aim is to conjure up an array as screen output on the corresponding jpg and the image should be rewritten with each call. What would be an alternative to BufferedImage, Graphics and ImageIO?
 
Tim Moores
Saloon Keeper
Posts: 6625
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I probably can't find a solution to my problem


I wouldn't say that. It would be very useful if you posted an SSCCE (<-- that's a link) so we could run the code ourselves. Right now we're just guessing what might be happening.

You could use distinctive filenames, maybe by incorporating a timestamp.
 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preparing my code still takes a while. Maybe it is helpful to use a github link first.

on Github

I will try to prepare and upload the code for SSCCE.

 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what to do at SSCCE. When I paste my Java code there, I get a lot of import errors. I have no idea what I have to do there, so I'll quickly give you my Github link again and hope that you can help with it.
Github

the code snipet can be found in the MenuSchreiber class

I've attached a few pictures that show what the output should look like. Since I would create too many files in this way (numbered filenames), I want the first file to be labeled again and again.
Again to the problem: The array text, which is passed as a parameter, arrives correctly, but is only written once to a file. Then I don't get this file rewritten in a program run.
It seems as if the menuBlanco2 file is still accessible.




 
Dave Tolls
Master Rancher
Posts: 4698
49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here's my SSCCE (you'll need to provide the image yourself):

And the final output in the menuBlanco2.jpg contains that last text (though I will point out that the first line is missing, presumably because the text is too high up on the "screen").
So it's not this code.
Your issue is somewhere else.
 
Dave Tolls
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to double check, I stuck a breakpoint on the second call to the method, and checked the image, which was the correct (first) output.
I kept the image open (in IntelliJ) and let the program exit and reopened the image, which had the new (final) state.

I also created a BufferedImage between the two calls, using the menuBlanco2 image as it existed at the time.
No issues there either. No surprise, as in neither case is the original file held open, and if it were I would expect some form of FileException when we tried to save the new version.

My best guess is that you are not reading in the new image.
 
Tim Moores
Saloon Keeper
Posts: 6625
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if I put a "Thread.sleep(10000);" between the two calls to createDisplayMessage, I can see that the first image is also created correctly. So yes, the code works fine.

Edit: ... which is what Dave just did, too. Great minds think alike :-)
 
Cora Taca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks. For now I have helped myself by creating new numbered files and then deleting them as soon as a new file was created. So I don't get any garbage. I'll take a closer look at the whole thing on another branch. it was also a big mystery to me why that didn't work. if the error is somewhere else, this naturally also extends the range of errors. I'll find him, thank you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic