PrintWriter out;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the text file that data will be saved to: ");
try
{
out = new PrintWriter(new BufferedWriter(new FileWriter( in.next())));
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
System.exit(0);
}
PrintWrite try-catch Exception
Page 1 of 110 Replies - 368 Views - Last Post: 09 December 2011 - 07:36 PM
#1
PrintWrite try-catch Exception
Posted 09 December 2011 - 05:49 PM
Hello, I am trying to write a code where I try to write into a file which my user chooses, in the try-catch section, there is never an error occurring although i have tried writing the file name as .txt and as a simple string with no extension but still got no error. Where is my mistake?
Replies To: PrintWrite try-catch Exception
#2
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 06:16 PM
So your complaining about not having errors? What is your ultimate goal? What is the actual outcome? Did you attempt to write to the file? If so, was the data written? According the code above, you didn't write anything. The code you have looks fine, except that you need to close your streams. This is particularly important for instances of PrintWriter. Close the PrintWriter object only; it will close its underlying streams.
This post has been edited by blackcompe: 09 December 2011 - 06:18 PM
#3
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 06:22 PM
This is one of the methods I have created in my code to open up a file to write to. I have tried writing to files and it has worked. The .txt files worked just fine and properly. The other files with no extension, when I change them to .txt in the folder they're located in, they open up properly with everything written inside. My problem or question is HOW can an error occur?! There has been no error so far but I'm sure there must be a way to find and catch an error that I just can't seem to figure out.
#4
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 06:50 PM
I'm quite confused. So this error you speak of is that when you write to a file with no extension (in its filename) and open it, you can't see its contents? E.g.
And when you open the file, nothing appears. Is that correct?
out = new PrintWriter(new BufferedWriter(new FileWriter("data")));
out.print("Some data");
out.close();
And when you open the file, nothing appears. Is that correct?
#5
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:02 PM
Well in a way that does happen, but when I change the extension to a text file it opens properly. But that isn't my issue. My question is when and how does an error occur? I mean when does the error message appear to the user? Writing a file name without an extension obviously did not cause an error to catch, when is there an error to catch?! I have tried several inputs but none of them give me errors and the way I know it, there's always a way for errors to occur. I would like to know where it's located here. Sorry if I'm being too confusing.
Here is my question as simple as I can put it: How can an error occur here that will get caught and terminate the program?
Here is my question as simple as I can put it: How can an error occur here that will get caught and terminate the program?
#6
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:10 PM
#7
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:11 PM
I understand. Java won't throw any errors at all about file names. The following are all valid:
data.txt
data
1.txt
data.data
Whatever files you can create in the OS's file explorer, you can create in Java. It sounds like your goal is to do validate the file name submitted by the user. It this correct?
data.txt
data
1.txt
data.data
Whatever files you can create in the OS's file explorer, you can create in Java. It sounds like your goal is to do validate the file name submitted by the user. It this correct?
This post has been edited by blackcompe: 09 December 2011 - 07:16 PM
#8
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:14 PM
Quote
What is here ?
Here in the try-catch block.
*Edited: quote tags fixed
This post has been edited by pbl: 09 December 2011 - 07:19 PM
#9
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:20 PM
an error can occur if you cause one of these exceptions:
try denying write access to an existing file and input that file
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
try denying write access to an existing file and input that file
#10
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:21 PM
Most I/O operations can throw an Execption
your filename may not be valid
you may not have write access to that directory
the disk might be full
...
your filename may not be valid
you may not have write access to that directory
the disk might be full
...
This post has been edited by pbl: 09 December 2011 - 07:22 PM
#11
Re: PrintWrite try-catch Exception
Posted 09 December 2011 - 07:36 PM
blackcompe I'm not sure that's my goal.
Yes, my required error is one of those. I have tried to name the file usin !@# and no error occured or appeared on the screen. I am worried that by me not being able to see the error message, my code might be wrong and when an error occurs it won't print out on the screen the message or terminate. It all appears to be in order or will the error be ignored if found?!
Thank you for your time and patience!
I don't know how to edit my post, but I have at last received an error. Thank you!
Yes, my required error is one of those. I have tried to name the file usin !@# and no error occured or appeared on the screen. I am worried that by me not being able to see the error message, my code might be wrong and when an error occurs it won't print out on the screen the message or terminate. It all appears to be in order or will the error be ignored if found?!
Thank you for your time and patience!
I don't know how to edit my post, but I have at last received an error. Thank you!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|