• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting some errors - java out of date?  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to write to a txt file using buffered writer - however I'm getting some issues.

BufferedWriter says 'The resource type BufferedWriter does not implement java.lang.AutoCloseable'

br says 'The type BufferedWriter is not visible'




Any idea why its doing that - can't find anything on it that's all
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using the try-with-resources statement. Have a read through the Oracle Tutorial: The try-with-resources Statement and see if that explains things for you.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah okay, cheers thank you! The code is off a course lecture I'm doing so wondered why his worked and mine didn't when they were the same
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should be fine.
I notice the name of your class is FileWriter, which is also the name of an existing Java class. I'm wondering if you have also written a BufferedWriter that is interfering with the java.io.BufferedWriter?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I re factored them and changed the names to just Read and Write



Should that work?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would, presumably, be quicker for you to test if it works?
Your original code worked.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn't compatible with the version of java I have - not allowed to update it so I had to change it.

I now have :


but that doesn't work still even though it's for the version of java I use. It says new BufferedWriter is undefined
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:It says new BufferedWriter is undefined

Don't paraphrase the error messages you get. Tell us what they say exactly.
In this case I believe you are actually getting the error
The constructor BufferedWriter(FileReader) is undefined
and that's because the BufferedWriter class does have a constructor that takes a FileReader as a parameter.
Take a look at your original code. What are you passing as a parameter there ?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sorry, thats the error I meant - ohhh I was using reader not writer. Thanks for pointing that out! Shouldn't:



and:



work fine though? It says bw cannot be resolved :S
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you can't resolve bw is that it has gone out of scope.

Since your friend insists on using a Java1.1 installation and you don't seem able to upgrade to anything written in your own lifetime, you won't be able to get try with resources to work. You are going to have to use the other form of the close instructionDon't try writing your own code tags. Use the button. I have sorted them out for you.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for all your help! It's solved now I can read and write my txt files completely fine
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And not only when they are written with goose quills
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of hours ago, I wrote: . . .. . .
If you look in the Java Language Specification about Exceptions, you find that the JVM must return all program state to its state at the beginning of the line on which the Exception occurred.
All exceptions (synchronous and asynchronous) are precise: when the transfer of control takes place, all effects of the statements executed and expressions evaluated before the point from which the exception is thrown must appear to have taken place. No expressions, statements, or parts thereof that occur after the point from which the exception is thrown may appear to have been evaluated.
As far as I can tell, that means that if an Exception is thrown from line 03 above, the Reader objects will not exists and the file will appear never to have been opened. In that case there will be no need to close the file. The close() method releases all system resources, so you only need one close() call.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!