DevanDev
DevanDev

Reputation: 355

How to populate the output of method in text file

I have a method Report Dose stat in Java:

 public String getReportDoseStat(){
    double maxdose=getMaxDose();
    String s=("Average dose:\t"+getAvDose()+"\n");
    s+=("Max dose:\t"+maxdose+"\n");
    s+=("Pixels 90% of max dose or more:\t"+getNmbrPixDose(maxdose*0.9)+"/"+getNmbrPixDose(0.0)+"\n");
    s+=("Pixels 50% of max dose or more:\t"+getNmbrPixDose(maxdose*0.5)+"/"+getNmbrPixDose(0.0)+"\n");
    s+=("Pixels 10% of max dose or more:\t"+getNmbrPixDose(maxdose*0.1)+"/"+getNmbrPixDose(0.0)+"\n");
    return s;
}

I would like to write the values generated from the this code to a table written in the method:

public void writeDosesTable(String p)// writing the dose table
{
    {
        PrintStream  fos;
        try{
            fos=new PrintStream(new File(p));
            String s;
            for(int j=0;j<nz;j++){
                s="";
                for(int i=0;i<nx;i++){
                    s+=det_els.get(j+i*nz).getDose()+";";// comma separated or Semicolon separated mentioned here
                }
                fos.println(s);
                // prints out the stream of  values in Doses table separated by Semicolon
            }
            fos.flush();
            fos.close();
        } 
        catch (IOException e){
            e.printStackTrace();
        }
        //finally 
        //{fos.close();}
    }
}

How could I possibly generate such a thing?

Upvotes: 1

Views: 94

Answers (1)

Mrunal Gosar
Mrunal Gosar

Reputation: 4683

Instead of having getDoseTable() method you can directly print the values in a file and while writing itself format your statements with the desired seperator. Something like below:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        String[] str = { "a", "b", "c" };
        BufferedWriter wr = new BufferedWriter(new FileWriter(new File(
                System.getProperty("user.dir") + File.separator + "test.csv")));
        for (String string : str) {
            wr.write(string + ",");
        }
        wr.flush();
        wr.close();
    }
}

Here String [] str , can be the string which you wish to write in a csv file delimited with certain delimiter and then while writing take care of where you are inserting the delimiter. Let me know if you need further assistance.

Upvotes: 1

Related Questions