Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FileOut.java

Go to the documentation of this file.
00001 //  -*- Java -*-
00002 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00010 
00013 package pkgUtil;
00014 
00015 import java.util.*;
00016 import java.io.*;
00017 import java.text.DateFormat;
00018 
00020 public class FileOut {
00021    private PrintWriter pStream = null;
00022    private int lineCount;       // count lines in current file
00023    private int fileCount;       // to mark a sequence of files
00024    private DateFormat myDateFormat; // future?
00025 
00026    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00030    public FileOut(){
00031       pStream = null;
00032       myDateFormat = DateFormat.getTimeInstance(DateFormat.FULL);
00033    }
00034 
00035    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00039    public int openFile( String fn ) {
00040       lineCount = 0;
00041       fileCount++;
00042       if( pStream != null ){
00043          this.closeFile();
00044       }
00045         
00046       try {
00047          pStream = new PrintWriter( new FileOutputStream( fn ));
00048       }
00049       catch(IOException e) {
00050          System.out.println("Error opening file " + fn );  
00051          return(1);
00052       }
00053       return 0;
00054    }
00055     
00056    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00060    public void closeFile(){
00061       if( pStream != null ){
00062          pStream.close();
00063       }
00064       pStream = null;
00065    }
00066     
00067    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00071    public boolean isOpen(){
00072       if( pStream != null ){
00073          return true;
00074       }
00075       return false;
00076    }
00077     
00078    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00082    public boolean putString( String s){
00083       if( pStream == null ){
00084          return false;
00085       }
00086       lineCount++;
00087       pStream.println(s);
00088       return true;              // ok
00089    }
00090 
00091    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00095    public int getLineCount(){
00096       return lineCount;
00097    }
00098 
00099    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00103    public int getFileCount(){
00104       return fileCount;
00105    }
00106 
00107    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00111    public void putChar( int ch){
00112       if( pStream != null ){
00113          pStream.write(ch);
00114       }
00115    }
00116 
00117    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00121    public void renameFile( String from ){
00122       
00123       // File (or directory) with old name
00124       File file = new File(from);
00125       
00126       // File (or directory) with new name
00127       String to = makeDatedName();
00128       File file2 = new File( to );
00129       
00130       // Rename file (or directory)
00131       boolean success = file.renameTo(file2);
00132       if (success) {
00133          System.out.println("renamed " + from + " to " + to );
00134       }
00135       else{
00136          System.out.println( to + " already exists..\n" + 
00137                              " Exit program and fix logs");
00138       }
00139       
00140    }
00141 
00142    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00146    private String makeDatedName(){
00147       Date theCurrentTime = new Date();
00148       String ds = myDateFormat.format(theCurrentTime);
00149       //return "LOG_" + ds.hashCode() + ".TXT";
00150       String s = ds.replace( ' ', '_');
00151       String t = s.replace( ':', '-');
00152 
00153       return "LOG_" + t + ".TXT";
00154    }
00155 
00156 } // class FileOut
00157 
00158 // -- end FileOut.java --

SourceForge.net Logo