Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help writing a text file

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Is this what you are saying to do:

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
BufferedWriter and FileWriter for a text file, surely?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What do you mean, BufferedWriter and FileWriter?
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
...and when I get that error message, it highlights the following line: P
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I haven't really learned exception handling yet, can you show me what you're talking about... thanks
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic