10 Replies - 1103 Views - Last Post: 09 December 2011 - 07:36 PM Rate Topic: -----

#1 javaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 11

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?


        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);
                }




Is This A Good Question/Topic? 0
  • +

Replies To: PrintWrite try-catch Exception

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

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

Was This Post Helpful? 0
  • +
  • -

#3 javaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

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.

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?
Was This Post Helpful? 0
  • +
  • -

#5 javaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 11

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?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: PrintWrite try-catch Exception

Posted 09 December 2011 - 07:10 PM

View Postjavaf, on 09 December 2011 - 10:02 PM, said:

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?

What is here ?
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

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?

This post has been edited by blackcompe: 09 December 2011 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 javaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 11

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

Was This Post Helpful? 0
  • +
  • -

#9 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: PrintWrite try-catch Exception

Posted 09 December 2011 - 07:20 PM

an error can occur if you cause one of these exceptions:

    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
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
...

This post has been edited by pbl: 09 December 2011 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#11 javaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1