-5

I am seeking to clarify what the Java API documentation states about the FileWriter class. The documentation states the following:

constructor:
Constructs a FileWriter object given a file name.

public FileWriter(String fileName)
    throws IOException


fileName - String The system-dependent filename.

IOException - if the named file exists but is a directory rather than a
regular file, does not exist but cannot be created, or cannot be opened
or any other reason

It is not clear to me whether or not the FileWriter object will attempt to create the file specified by the fileName String, although it is clear that the object will check to see if the file is created and an exception thrown if it can not be created.

  • 2
    Yes, it does. Hint for the future: if you're not sure, it doesn't take long to test things like this... – Jon Skeet Mar 13 '15 at 17:37
  • 1
    I guess this q&a will help you. http://stackoverflow.com/questions/8630484/why-new-filewriterabc-txt-creates-a-new-file-and-new-fileabc-txt-does-no – jungyh0218 Mar 13 '15 at 17:38
  • The doc says: *throws IOException if the named file [...] does not exist but cannot be created*. Doesn't that imply that the method tries creating the file if it doesn't exist? – JB Nizet Mar 13 '15 at 17:40
  • @JonSkeet perhaps not, but does that make it a bad question? –  Mar 13 '15 at 17:40
  • @JBNizet, no it does not imply that. The docs say, it will throw the error if it does not exist and can not be created. It does not say that it will attempt to create the file. –  Mar 13 '15 at 17:45
  • 1
    So, by your logic, the method would test if the file can be created without any intention of creating the file, just to lose some time and throw an unrelated exception? – JB Nizet Mar 13 '15 at 17:50
  • @JBNizet not by my logic. By what is said in the documentation, it is not stated that FileWriter attempts to create the file or not. I asked the question because I did not want to assume that file creation was attempted. As you probably know, a fact such as attempting to create a file is often explicitly stated in documentation (it was not in this case). –  Mar 13 '15 at 17:57
  • 1
    @not__p: Yes, I think so. It shows ab inclination to use SO as a *first* port of call, instead of doing adequate research first. – Jon Skeet Mar 13 '15 at 18:02
  • @JonSkeet, I suppose that's true. But I would guess that others would like to know the answer to this question and I thought it would be nice to have it answered here. I guess that cost me some rep points. –  Mar 13 '15 at 18:07
  • @JonSkeet I'd like to add that I've been a member here for two and a half years now and only asked 6 questions to this community. –  Mar 13 '15 at 18:09
  • 1
    I don't think that makes it a better question in any way. To put it another way: I would never ask a question which I hadn't spent *at least* half an hour struggling with. Just writing the question usually takes me a considerable time, to make sure it's the best question it can be. If the answer to a question can be determined in less than two minutes (which this can), I don't think it's worth asking. To put it another way - this comment is three times the length of the Java program required to test it one way or another. – Jon Skeet Mar 13 '15 at 18:15
  • @JonSkeet I see your point and I agree. In my last comment, I meant to demonstrate that is not my habit to do that. I turn to documentation to answer these sorts of questions so that time and keystrokes can be saved by not needing to test. I figured if I could save others that time by clarifying the documentation (programmer time is usually the most expensive element in projects) then the community would be better off -- spending a lot of time on this question though ;-) –  Mar 13 '15 at 18:24
  • 1
    A further benefit of trying something out first is you can demonstrate what you've done so far. Demonstrating due diligence and walking readers through what you've done so far is often the primary difference between a "good" and a "bad" question. If you'd worked through this question, tried out several different cases, and posted saying "I've tried all these things, but it's still not clear to me how this works" you'd probably have a well received question, even if the topic is the same. – dimo414 Mar 13 '15 at 20:09
  • @JonSkeet A more formal argument for the value of my question is [here](http://meta.stackoverflow.com/questions/288008/are-questions-that-clairify-api-documentation-helpful-and-therefore-a-good-fit-f/288009#288009). I accepted an answer already (as it was the only one offered after 15 views), but I think I can still change that if convinced otherwise. I could have answered my question in a time frame approaching two minutes by writing Java code, but not is less time than a google query and well indexed response (which is where most people would start to find an answer to this question). –  Mar 13 '15 at 20:13
  • @not__p: Well, that's Bradley's view - and I take a different one, particularly for things that are so easily tested. There are plenty of places where documentation can be hard to understand or incomplete in ways where it's not guaranteeing something that might be tricky to infer. In this case, it's really simple, I personally think that while the documentation isn't as clear as it might be, the answer can still reasonably be inferred from it, and generally the question doesn't help. – Jon Skeet Mar 13 '15 at 20:16
  • @dimo414, following your suggestion every programmer with this question would perform a test to clarify the documentation. Why not save those in our community the the time by providing clarification of the API here? –  Mar 13 '15 at 20:17
  • @JonSkeet while I appreciate your restatement of your opinion, you do not offer any objective reason why my question is of low quality. Why should documentation not be clarified where possible? Why should we perform these tests when they can be avoided my doing such clarification? –  Mar 13 '15 at 20:22
  • @not__p: Documentation should indeed be clarified - so I would suggest filing a bug with Oracle. I don't think it's useful as a question, and encourages further research-free questions. (I've seen plenty of questions asking what a method does where the documentation *does* explicitly state it.) Anyway, this discussion is now off the top of the question itself - I'm not going to add any more comments to it; you can do so, of course... – Jon Skeet Mar 13 '15 at 20:26
  • 1
    There is certainly value to questions that clarify documentation and help save people some time. Like I said, your question could have been a valuable one, if you'd demonstrated due diligence. Since you asked the question, you have a higher burden than people who just wonder about it, because you're expecting others to take the time to answer you. If you demonstrate no effort in your question, you'll receive no-effort responses. You could, for instance, have written your question *and* researched the problem and answered it yourself. That would surely have been well received. – dimo414 Mar 13 '15 at 20:35
  • @dimo414 Thank you. That bit of education cost me 4 down votes. I guess that's the price of becoming a better poster. Hover over the down vote button and the see this: "does not show **any** research effort, [question] is unclear or not useful." My research, while perhaps lacking, is demonstrated my initial post and it can not be said that my question does not show **any** research, and I have offered proof of the usefulness. Notwithstanding that, this question will still be answered for all those who seek the same clarification as I did. Accordingly, I up voted your comment as useful. –  Mar 13 '15 at 20:55

1 Answers1

2

Yes, it will be created (if it does not already exist.) If the file cannot be created, an IOException is thrown.

Adam Fontana
  • 106
  • 1
  • 7