Login  Register

Re: how to read in a text file in a plugin

Posted by Robert Dougherty on Oct 09, 2005; 6:36pm
URL: http://imagej.273.s1.nabble.com/how-to-read-in-a-text-file-in-a-plugin-tp3704664p3704666.html

Max,

Your question has several different answers depending on the nature of the
text data, what you need to do with it, and what style of programming you
prefer.  One of the lower-level approaches is illustrated below.  If you
have a text image, you might be able just use
  IJ.run("Text Image... ", "open=D:\\filename.txt");

Bob


Robert P. Dougherty, Ph.D.
President, OptiNav, Inc.
Phone (425) 467-1118
Fax (425) 467-1119
www.optinav.com


import ij.*;
import ij.gui.*;
import ij.plugin.PlugIn;
import java.awt.*;
import java.io.*;
import java.util.*;;

public class Data_Reader implements PlugIn {
        public void run(String arg) {
                float[][] data = readSpaceDelimNumbers("Read data file");
                IJ.write(data.length+" lines of data read.");
        }
        public float[][] readSpaceDelimNumbers(String prompt) {
                Frame fram = new Frame();
                String dialogPrompt = new String("Space delimted numerical
file");
                if (prompt != null){
                        dialogPrompt = prompt;
                }
                FileDialog fd = new
FileDialog(fram,dialogPrompt,FileDialog.LOAD);
                fd.setVisible(true);
                String path = fd.getDirectory();
                String filename = fd.getFile();
                if ((path == null) || (filename == null)) {
                        return null;
                }
                Vector list = new Vector(0, 16);
                try {
                        FileReader fr = new FileReader(path + filename);
                        BufferedReader br = new BufferedReader(fr);
                        String line;
                        list = new Vector(0, 16);

                        do {
                                line = br.readLine();
                                if (line != null){
                                        StringTokenizer st = new
StringTokenizer(line);
                                        if(st.hasMoreTokens())
                                                list.addElement(st);
                                }
                        } while (line != null);
                        fr.close();
                } catch (FileNotFoundException e) {
                        IJ.error("File not found exception");
                } catch (IOException e) {
                        IJ.error("IOException exception");
                }
                int n = list.size();
                float[][] data = new float[n][];
                for (int i = 0; i < n; i++){
                        StringTokenizer st =
(StringTokenizer)list.elementAt(i);
                        int nd = st.countTokens();
                        data[i] = new float[nd];
                        for (int j = 0; j < nd; j++){
                                String part = st.nextToken();
                                try {
                                        //data[i][j] =
Double.valueOf(part).floatValue();
                                        data[i][j] =
Float.valueOf(part).floatValue();
                                }
                                catch (NumberFormatException e) {
                                }
                        }
                }
                return data;
        }
}
 

> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Xiaoyan Ma
> Sent: Saturday, October 08, 2005 11:19 AM
> To: [hidden email]
> Subject: how to read in a text file in a plugin
>
> I am new to write plugins. Could someone here help me how to read in a
> text file( saved data) into a plugin. It seems I could use getNumber().
> But don't know how?
>
>
> Thanks. Max