• Post Reply 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

Writing To A File, Prefered Procedure

 
Greenhorn
Posts: 1
Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just recently taken to trying to learn Java, and I've been reading up on some books and viewing the pages and pages of documentation. I've been programming in C for 10+ years now and I know the pitfalls of developing bad habits when learning something new, so I'm hoping the Java experts here can help guide me in the ways I'm doing things wrong, how to do things better, and hopefully explain some of the inner workings of Java.

Anyway, I've been working on creating some simple Java programs trying to gain familiarity with the language, and with regards to writing to a file, I'm curious as the best way to go about it?



This is what I wrote up.

Any significant blemishes here?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, just a few minor things to be aware of.

1) Relative paths. Just like in other contexts, without a leading "/" for unix or "C:/", for Windows, or whatever indicates and absolute path in your file system, the file name will be relative to the user's current working directory. This can lead to confusion, as beginners often assume there's some relationship between that directory and where the main class lives. There's not. Just like executing any other program it's where you current "are" when you invoke the java executable. This is really an OS/shell/file system issue, rather than a Java one, but class files and classpath seem to confuse people.

2) You didn't call newLine(), or whatever BR's method of that nature is. Not a problem here, but if you call write() again, the two will be run together.

3) No need for an explicit flush() here, as close() calls flush().

4) This is the only significant error I see: You should always call close() in a finally block. Such as:
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

There is one flaw in your code - you may not always close the file. If an exception occurs while writing or flushing, the file will not be closed. By using a try-finally block (or try-with-resources since Java 7) you can ensure that it will:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:


<nitpick>
You should wrap the close() call in a try/catch. You don't want an exception in close() to change the way the overall try statement completes.

 
reply
    Bookmark Topic Watch Topic
  • New Topic