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);
}
}
No comments:
Post a Comment