Tuesday, January 29, 2013

Learning Java and a fileWriteUtils() class


As a companion to that fileUtils() class that in retrospect should have been named fileReadUtils() here is the fileUtilsWrite(). Class naming convention was broken as this should have been (and will soon be) named as fileWriteUtils().

File name and class name convention is pretty important to maintain. Makes development easier as there is less to remember. You don't need to figure out, "well is it fileUtilsRead() or fileReadUtils()...all other classes end in "Util" should I make an exception here"? So catching these early on and making the effort to achieve overall coding consistency is the way to go.

And of course once this is instantiated as:


fileUtilsWrite fileUtW = new fileUtilsWrite("fullPath","string to write");

Off it goes, runs fine and writes the string to that file on the C drive. It's pretty clear that there are two Strings being sent to the fileUtilsWrite() and those are not being used, just a heads up. Using this inside of a larger program, I suppose what needs to happen is:

1). Figure out the file name (calling program would need to manage that).
2). Assemble the String that needs to be written.
3). Either instantiate or call the class.

That's it.

Here is the code:


public class fileUtilsWrite {
    public fileUtilsWrite(String outFileName, String outFileText){

    String strFilePath = "C://Users//Frank//WriteStringAsChars.txt";
    String outString = "hello world-\n";
    outString = outString + "                string 2, -\n";
    outString = outString + "                string3, -\n";
    
   try
   {
       FileWriter fstream = new FileWriter(strFilePath);
       BufferedWriter out = new BufferedWriter(fstream);
       out.write(outString);
       out.close();
    }
    catch (IOException e)
    {
       System.out.println("IOException : " + e);
    }
}


Saturday, January 26, 2013

Learning Java and a fileUtils() class

And it is 2013 and this is the first blog entry of the year. There was a urban conditioning hike that happened, more on that in a separate blog label.


Continuing on with the re-learning Java topic, seems I needed a small program that read the files in a directory and placed those file names and their full qualifying path name into a jTextField. So as this was being coded I thought, good time to add to that to a fileutils() class, later decided a fileUtilsWrite() class is needed too.
As things will have it, this fileUtils class will just return the filenames from the directory. As a prototype class, it is fairly instructive.This particular file does in fact NOT return the full qualifying path name of the files. That code went into yet another Netbeans project.



Writing this fileUtils class went pretty quickly. Most of that is a cut/paste from the Internet (why re-invent the wheel). The challenge was that the calling program, or the uitest.java (as it is called) needs to be able to read off the file list one by one. There were a few complications so I settled on reading in the files into a vector (vc), then using the object oriented nature to pick these file names off, one by one. This is how that is done:


        System.out.println("uitest: " + fileUt.getSize());
        for(int i=0;i<fileUt.getSize();i++){
           System.out.println("uitest: " + fileUt.getFileName(i));
        }

Here is the code for reading the directory and placing the filenames into a vector.

public class fileUtils {
   public fileUtils(String szendsWith){
   System.out.println("fileUtils:");
   
   File folder = new File("c:/");
   File[] listOfFiles = folder.listFiles();

   for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
         System.out.println("File: " + listOfFiles[i].getName());
      } 
      else if (listOfFiles[i].isDirectory()) {
              System.out.println("Directory: " + listOfFiles[i].getName());
      }
      if(listOfFiles[i].getName().endsWith(szendsWith)){
         System.out.println("File: " + listOfFiles[i].getName());
         vc.add(listOfFiles[i].getName());
      }
   } 
   //System.out.println("fileUtils: " + vc.get(1));
}

   public int getSize(){
       size=vc.size();
       return size;
   }
    
   public String getFileName(int i){
       String fileName;
       fileName=vc.get(i).toString();
       return fileName;
   }
    
    private int size;
    private String szFileName;
    public  Vector vc = new Vector();
    
}

But wait, there's more to come, it is the fileUtilsWrite() class.