/*
*   Class FileOutput
*
*   Methods for writing doubles, floats, BigDecimals,
*   integers, long integers, short integers, bytes,
*   Strings, chars, booleans, Complex, Phasor,
*   ErrorProp and ComplexErrorProp to a text file.
*
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:    July 2002
*   UPDATED: 26 April 2004, 22 January 2006, 27 June 2007, 21-23 July 2007, 1 February 2009
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's Java library on-line web page:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/FileOutput.html
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2002 - 2009
*
*   PERMISSION TO COPY:
*
*   Redistributions of this source code, or parts of, must retain the above
*   copyright notice, this list of conditions and the following disclaimer.
*
*   Redistribution in binary form of all or parts of this class, must reproduce
*   the above copyright, this list of conditions and the following disclaimer in
*   the documentation and/or other materials provided with the distribution.
*
*   Permission to use, copy and modify this software and its documentation for
*   NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
*   to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all
*   copies and associated documentation or publications.
*
*   Dr Michael Thomas Flanagan makes no representations about the suitability
*   or fitness of the software for any or for a particular purpose.
*   Michael Thomas Flanagan shall not be liable for any damages suffered
*   as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/

package flanagan.io;

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

import flanagan.complex.*;
import flanagan.circuits.Phasor;
import flanagan.analysis.ErrorProp;

public class FileOutput{

        // Instance variables
        private String filename = "";       // output file name
        private FileWriter fwoutput = null; // instance of FileWriter
        private PrintWriter output = null;  // instance of PrintWriter
        private boolean append = false;     // true data appended to a file, false new file
        private char app = 'w';             // 'w' new file - overwrites an existing file of the same name
                                            // 'a' append to existing file, creates a new file if file does not exist
                                            // 'n' adds a number to file name. If file name of that number exists creates a file with next highest number added to name
        private boolean fileExists = true;  // = false if file of output filename does not already exist

        // Constructors

        public FileOutput(String filename, char app){
                this.filename = filename;
                this.app = app;
                this.setFilenames(filename, app);
        }

        public FileOutput(String filename, String apps){
                this.filename = filename;
                this.app = apps.charAt(0);
                this.setFilenames(filename, app);
        }

        public FileOutput(String filename){
                this.filename = filename;
                this.app = 'w';
                this.setFilenames(filename, app);
        }

        // Methods

        // Set the file names
        private void setFilenames(String filename, char app){
                BufferedReader input0;
                try{
                    input0 = new BufferedReader(new FileReader(filename));
                }catch(FileNotFoundException e){
                    this.fileExists = false;
                }

                if(this.app == 'n'){
                        boolean test = true;
                        int i = 0;
                        BufferedReader input;
                        String ext = "";
                        String filename0 = "";

                        int idot=filename.indexOf('.');
                        if(idot!=-1){
                                ext += filename.substring(idot);
                                filename0 += filename.substring(0,idot);
                        }
                        else{
                                filename0 += filename;
                        }

                        while(test){
                                i++;
                                filename=filename0+String.valueOf(i)+ext;
                                try{
                                        input = new BufferedReader(new FileReader(filename));
                                }catch(FileNotFoundException e){
                                        test=false;
                                        this.filename=filename;
                                }
                        }
                }

                if(this.app == 'a'){
                        this.append=true;
                }
                else{
                        this.append = false;
                }
                try{
                        fwoutput = new FileWriter(filename, this.append);
                }
                catch(IOException e){
                        System.out.println(e);
                }

                this.output = new PrintWriter(new BufferedWriter(fwoutput));
        }

        // Return filename
        public String getFilename(){
            return this.filename;
        }

        // Return file already exists check
        // returns true if it does, false if it does not
        public boolean checkFileAlreadyExists(){
            return this.fileExists;
        }


        // PRINT WITH NO FOLLOWING SPACE OR CHARACTER AND NO LINE RETURN

        // Prints character, no line return
        public final synchronized void print(char ch){
                this.output.print(ch);
        }

        // Prints character, no line return, fixed field length
        public final synchronized void print(char ch, int f){
                String ss ="";
                ss = ss + ch;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints string, no line return
        public final synchronized void print(String word){
                this.output.print(word);
        }


        // Prints string, no line return, fixed field length
        public final synchronized void print(String word, int f){
                String ss ="";
                ss = ss + word;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }
        // Prints double, no line return
        public final synchronized void print(double dd){
                this.output.print(dd);
        }

        // Prints double, no line return, fixed field length
        public final synchronized void print(double dd, int f){
                String ss ="";
                ss = ss + dd;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints float, no line return
        public final synchronized void print(float ff){
                this.output.print(ff);
        }

        // Prints float, no line return, fixed field length
        public final synchronized void print(float ff, int f){
                String ss ="";
                ss = ss + ff;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints BigDecimal, no line return
        public final synchronized void print(BigDecimal big){
                this.output.print(big.toString());
        }

        // Prints BigDecimal big, no line return, fixed field length
        public final synchronized void print(BigDecimal big , int f){
                String ss ="";
                ss = ss + big.toString();
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints BigInteger, no line return
        public final synchronized void print(BigInteger big){
                this.output.print(big.toString());
        }

        // Prints BigInteger big, no line return, fixed field length
        public final synchronized void print(BigInteger big , int f){
                String ss ="";
                ss = ss + big.toString();
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints Complex, no line return
        public final synchronized void print(Complex ff){
                this.output.print(ff.toString());
        }

        // Prints Complex, no line return, fixed field length
        public final synchronized void print(Complex ff, int f){
                String ss ="";
                ss = ss + ff;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints Phasor, no line return
        public final synchronized void print(Phasor ff){
                this.output.print(ff.toString());
        }

        // Prints Phasor, no line return, fixed field length
        public final synchronized void print(Phasor ff, int f){
                String ss ="";
                ss = ss + ff;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints ErrorProp, no line return
        public final synchronized void print(ErrorProp ff){
                this.output.print(ff.toString());
        }

        // Prints ErrorProp, no line return, fixed field length
        public final synchronized void print(ErrorProp ff, int f){
                String ss ="";
                ss = ss + ff;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints ComplexErrorProp, no line return
        public final synchronized void print(ComplexErrorProp ff){
                this.output.print(ff.toString());
        }

        // Prints ComplexErrorProp, no line return, fixed field length
        public final synchronized void print(ComplexErrorProp ff, int f){
                String ss ="";
                ss = ss + ff;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints int, no line return
        public final synchronized void print(int ii){
                this.output.print(ii);
        }

        // Prints int, no line return, fixed field length
        public final synchronized void print(int ii, int f){
                String ss ="";
                ss = ss + ii;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints long integer, no line return
        public final synchronized void print(long ll){
                this.output.print(ll);
        }

        // Prints long integer, no line return, fixed field length
        public final synchronized void print(long ll, int f){
                String ss ="";
                ss = ss + ll;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints short integer, no line return
        public final synchronized void print(short ss){
                this.output.print(ss);
        }

        // Prints short integer, no line return, fixed field length
        public final synchronized void print(short si, int f){
                String ss ="";
                ss = ss + si;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints byte integer, no line return
        public final synchronized void print(byte by){
                this.output.print(by);
        }

        // Prints short integer, no line return, fixed field length
        public final synchronized void print(byte by, int f){
                String ss ="";
                ss = ss + by;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints boolean, no line return
        public final synchronized void print(boolean bb){
                this.output.print(bb);
        }

        // Prints boolean, no line return, fixed field length
        public final synchronized void print(boolean bb, int f){
                String ss ="";
                ss = ss + bb;
                ss = FileOutput.setField(ss,f);
                this.output.print(ss);
        }

        // Prints array of doubles, no line return
        public final synchronized void print(double[] array){
                int n = array.length;
                for(int i=0; in){
                    for(int i=n+1; i<=f; i++){
                        ss=ss+sp;
                    }
                }
                return ss;
        }

    // Delete a file
    public static void deleteFile(String fileName){

        // deletion flag
        boolean flag = true;

        // Create an instance of File representing the named file
        File file0 = new File(fileName);

        // Check that the file exists
        if(!file0.exists()){
            System.out.println("Method deleteFile: no file or directory of the name " + fileName + " found");
            flag = false;
        }

        // Check whether FileName is write protected
        if(!file0.canWrite()){
            System.out.println("Method deleteFile: " + fileName + " is write protected and cannot be deleted");
            flag = false;
        }

        // Check, if fileName is a directory, that it is empty
        if(file0.isDirectory()){
            String[] dirFiles = file0.list();
            if (dirFiles.length > 0){
                System.out.println("Method deleteFile: " + fileName + " is a directory which is not empty; no action was taken");
                flag = false;
            }
        }

        // Delete fileName
        boolean deleteDone = file0.delete();

        if (!deleteDone){
            System.out.println("Method deleteFile: deletion of the file " + fileName + " failed");
        }
    }

}

 

Ads help maintain this website.