This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Good morning, everybody. As you will see from the following code, I am writing my surface gravity data to a text file. However, I am wondering if I can use a static method to write this data to a text file and call it back in the main method. Can I do this, and if so, how would I go about doing that?
You can do that with a static method, if everything you want can be sent to it. You could have a Utilities class with lots of static methods in. Create one with this heading
public static boolean writeToFile(String fileName, boolean append, String contents)
Look in the FileWriter constructors to see what you can use the boolean append for.
Use the file name to create the Writers.
Iterate through the array and write one line at a time.
Return true if it worked, and throw an Exception or return false if it didn't work.
Add the exception handling, or declare it with a "throws".
But using java.util.Formatter might be easier. There is only one problem with Formatter: as far as I can tell it always starts at the first line; I have never found a way to persuade it to append to an existing file.
Joined: Jan 13, 2009
Ok. Thanks for the suggestions. I now have the following:
However, it only writes the last value of calculateSurfaceGravity to the text file. I have printToFile in a for loop, so it should get all of the values out and printed to a text file. However, it is only getting the last one. Any idea why it is doing this or how I can get it to print ALL of the surface gravity values to the text file? Thanks
In this method, you have create object of PrintWriter PrintWriter q = new PrintWriter(new FileOutputStream("Summary.txt"), true);
So, let consider that:
When you use new PrintWriter, you open the file and write on it,
you do it again, then you write new value on that file, the old value is no more.
So finally you got the last value.
And if you are using a static method, you don't want to do any calculations in that method. You take the input in an appropriate format (I earlier suggested String) and write it to the file. That's it. Nothing else. No calculating temperatures, just file writing.
David Barry wrote:...and when I get that error message, it highlights the following line: P
This line can throw the file not found exception -- which is a checked exception. You are required to either handle the exception, or pass the buck, by declaring that your method can throw it..... BTW, have you learned exception handling yet?
I haven't really learned exception handling yet, can you show me what you're talking about... thanks
Joined: Oct 13, 2005
If you are writing text, well, Formatter is probably the easiest. Otherwise use the old-fashioned way of writing, shown here with appropriate exception handling. All parts will work in Java1.4 and Java1.3. You may be able to enhance it with a for-each loop or static imports if using Java5 or Java6.I am writing from memory, so you will have to check. There should be more details in the Java tutorials. I still think you will find Formatter easier to use (java5 and Java6 only).
BTW, I am pretty sure someone is going to show you a 2 line solution that will allow your program to compile. Please read and understand the tutorial instead. The goal is to understand and correctly use exceptions -- not to circumvent it so that your program can compile.