wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Need help writing a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Need help writing a text file

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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.
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
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
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26


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.

Solution:


and


Try it

Phuc Bui
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Is this what you are saying to do:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
BufferedWriter and FileWriter for a text file, surely?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
What do you mean, BufferedWriter and FileWriter?
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
I have this:



When I run it, though, I get an error message that says: "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown." What could be causing this?
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
...and when I get that error message, it highlights the following line: P
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
I haven't really learned exception handling yet, can you show me what you're talking about... thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

David Barry wrote:I haven't really learned exception handling yet, can you show me what you're talking about... thanks


Well, the Sun tutorial on exceptions is a good place to start...

http://java.sun.com/docs/books/tutorial/essential/exceptions/


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.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help writing a text file