Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

Thread: "Writing to a text file" not compiling

  1. #1
    xpot is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question "Writing to a text file" not compiling

    It is showing 'file not found exception' in the line in which the 'output' object is declared.The file 'scores.txt' is in my current working directory also.I cant get what's the error there.Please help.





    package file8;

    import java.io.*;
    public class File8 {

    public static void main(String[] args) {
    File file = new File("scores.txt");
    if (file.exists()) {
    System.out.println("File already exists");
    System.exit(1);
    }



    PrintWriter output = new PrintWriter(file);

    output.print("John T Smith ");
    output.println(90);
    output.print("Eric K Jones ");
    output.println(85);

    output.close();
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: "Writing to a text file" not compiling

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "Writing to a text file" not compiling

    Before you try to open the file for writing, try doing a System.out.println() of both the file.getAbsolutePath() and file.canWrite(). What do those two output?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: "Writing to a text file" not compiling

    I bet your compiler protested in the strongliest way against not handling IOExceptions anywhere ...

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: "Writing to a text file" not compiling

    "If the given file object 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."
    From the API.
    So there's a few possibilities there and, since you have an exists() check which exits if the file exists, the relevant ones are in italics.

    But, as Jos implies, it would be nice to see the actual code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "Writing to a text file" not compiling

    Well I guess that is the code; the title of the post already indicates "not compiling". I'm guessing that the reference to the file not found exception is not during runtime, but actually during compile time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: "Writing to a text file" not compiling

    Ahhhh!
    In other words it'd complaining that the OP is not handling the FNF exception!

    In which case they need to read the Exceptions Tutorial.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "Writing to a text file" not compiling

    ... and provide the exact error gotten in stead of "something along these lines" :/ If the OP would have specified "unhandled exception type FileNotFoundException" then there would have been no confusion.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    xpot is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: "Writing to a text file" not compiling

    Thanks a lot.I removed the if block & it still showing "FileNotFoundException".but that file lies in my current working directory.

  10. #10
    xpot is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: "Writing to a text file" not compiling

    Ya.It's compile time

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: "Writing to a text file" not compiling

    Did you read the tutorial suggested by Tolls?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Replies: 0
    Last Post: 06-14-2011, 07:52 AM
  4. Replies: 1
    Last Post: 06-30-2009, 03:32 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •