How to read and write files using a java plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to read and write files using a java plugin

Avital Steinberg
Hi,
My plugin has a function (function A) that calculates results that I would
like to save in a file. These results will be used by another function,
which means that function B has to read this file (that should be chosen by
the user). Function B will use the parameters that were saved in it to do
calculations.

It would have been easy to do with the ImageJ macro, but I am trying to do
it in a Java plugin. What is the best way to do it?

Thank you,
Avital

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to read and write files using a java plugin

C Heeney
Avital

If the results from Function A will be used in Function B, why not just pass the results directly from Function A to Function B.?

If it's the case that your data is complicated (e.g. it contains more than one data type), then create a class which handles these data types and pass it between functions.

Have a look at this tutorial on classes.
http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Best wishes
C Heeney

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to read and write files using a java plugin

Avital Steinberg
Thanks - I would like to give the user the option to use results that were
saved in a file a while ago, so what I did is similar to what you
recommended - I created a class that handles it. As a computational
chemist, I was looking for a shortcut similar to these shortcuts:
http://rsb.info.nih.gov/ij/developer/macro/functions.html

Some of these shortcuts can also be used in Java plugins, but I haven't
found an analog for imageJ macro commands such as: File.openDialog(title).
If there is a global way to convert imageJ macro commands to Java commands
(I am using the ImageJ library), it would be very nice.

Best wishes,

Avital


On Fri, Aug 29, 2014 at 12:42 PM, C Heeney <[hidden email]> wrote:

> Avital
>
> If the results from Function A will be used in Function B, why not just
> pass the results directly from Function A to Function B.?
>
> If it's the case that your data is complicated (e.g. it contains more than
> one data type), then create a class which handles these data types and pass
> it between functions.
>
> Have a look at this tutorial on classes.
> http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
>
> Best wishes
> C Heeney
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to read and write files using a java plugin

Michael Schmid
In reply to this post by Avital Steinberg
Hi Avital,

if you know what to write in a macro, here is a simple way how to find the corresponding commands for a plugin:

- Have a look at the static IJ class. Many macro commands have equivalents there.
- If you don't find it in IJ, have a look at ij.macro.Functions.java. There you will find the code executed upon calling one of the ImageJ built-in macro functions.

Example:

Reading a file into a string, macro command File.openAsString(path):
   IJ.openAsString(String path)

For writing a file, you may find some code in ij.macro.Functions.openFile(), closeFile() etc:

   FileOutputStream fos = new FileOutputStream(path);
   BufferedOutputStream bos = new BufferedOutputStream(fos);
   PrintWriter  writer = new PrintWriter(bos);

   writer.println(myString);

   writer.close();

---

Another option is using java.util.Properties; this is essentially a Hashtable that can be used to nicely save pairs like
  myKey1=myValue1
  myKey2=myValue2
or an XML file ('flat' XML file with key-value pairs only, no hierarchies). Values must be Strings, but you can convert them back to numbers with, e.g., ij.utils.parseDouble.

In that case, you can call ij.io.SaveDialog.java and ij.io.OpenDialog.java for the file dialogs, then create the java.io.FileOutputStream.html and FileInputStream streams required by the Properties.store and Properties.load.

Michael
________________________________________________________________
On Aug 28, 2014, at 10:40, Avital Steinberg wrote:

> Hi,
> My plugin has a function (function A) that calculates results that I would
> like to save in a file. These results will be used by another function,
> which means that function B has to read this file (that should be chosen by
> the user). Function B will use the parameters that were saved in it to do
> calculations.
>
> It would have been easy to do with the ImageJ macro, but I am trying to do
> it in a Java plugin. What is the best way to do it?
>
> Thank you,
> Avital

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to read and write files using a java plugin

Avital Steinberg
Thank you - this answer is very helpful,

Avital


On Mon, Sep 1, 2014 at 7:12 PM, Michael Schmid <[hidden email]>
wrote:

> Hi Avital,
>
> if you know what to write in a macro, here is a simple way how to find the
> corresponding commands for a plugin:
>
> - Have a look at the static IJ class. Many macro commands have equivalents
> there.
> - If you don't find it in IJ, have a look at ij.macro.Functions.java.
> There you will find the code executed upon calling one of the ImageJ
> built-in macro functions.
>
> Example:
>
> Reading a file into a string, macro command File.openAsString(path):
>    IJ.openAsString(String path)
>
> For writing a file, you may find some code in
> ij.macro.Functions.openFile(), closeFile() etc:
>
>    FileOutputStream fos = new FileOutputStream(path);
>    BufferedOutputStream bos = new BufferedOutputStream(fos);
>    PrintWriter  writer = new PrintWriter(bos);
>
>    writer.println(myString);
>
>    writer.close();
>
> ---
>
> Another option is using java.util.Properties; this is essentially a
> Hashtable that can be used to nicely save pairs like
>   myKey1=myValue1
>   myKey2=myValue2
> or an XML file ('flat' XML file with key-value pairs only, no
> hierarchies). Values must be Strings, but you can convert them back to
> numbers with, e.g., ij.utils.parseDouble.
>
> In that case, you can call ij.io.SaveDialog.java and ij.io.OpenDialog.java
> for the file dialogs, then create the java.io.FileOutputStream.html and
> FileInputStream streams required by the Properties.store and
> Properties.load.
>
> Michael
> ________________________________________________________________
> On Aug 28, 2014, at 10:40, Avital Steinberg wrote:
>
> > Hi,
> > My plugin has a function (function A) that calculates results that I
> would
> > like to save in a file. These results will be used by another function,
> > which means that function B has to read this file (that should be chosen
> by
> > the user). Function B will use the parameters that were saved in it to do
> > calculations.
> >
> > It would have been easy to do with the ImageJ macro, but I am trying to
> do
> > it in a Java plugin. What is the best way to do it?
> >
> > Thank you,
> > Avital
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html