Results 1 to 11 of 11
  1. #1
    jleyland96 is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default PrintWriter not working, FileNotFound Exception!

    Hi

    I have done lots of PrintWriting before but this is the first time I have done it on my new computer and I'm having an error I haven't seen before. This is my code:


    Rprint(){
    try {


    PrintWriter pout = new PrintWriter(new FileWriter("‪C:\\Users\\Jack\\Documents\\example.r "));

    pout.println("nums = c(1,2,5,6,8,10)");
    pout.println("nums");
    pout.close();

    } catch (IOException ex) {
    System.out.println("Error!");
    Logger.getLogger(Rprint.class.getName()).log(Level .SEVERE, null, ex);
    }
    }


    *** java.io.FileNotFoundException: ‪C:\Users\Jack\Documents\this.txt (The filename, directory name or volume label syntax is incorrect) ***

    If anyone has any idea what the problem is (because this directory definitely exists), please let me know! I have a feeling it may be to do with permissions, because it lets me print simply ("example.r") into my project folder.

    THANKS

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: PrintWriter not working, FileNotFound Exception!

    The error message does not correspond to the code example you give: this.txt != example.r

    Probably unrelated: there is an extra space after .r in your code example. I'm pretty sure you don't need that.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: PrintWriter not working, FileNotFound Exception!

    Not certain. I wonder if trailing white space is causing problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by jim829 View Post
    Not certain. I wonder if trailing white space is causing problem.

    Regards,
    Jim
    Since most modern filesystems, including Windows ones, allow whitespace in paths the answer is very likely yes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by gimbal2 View Post
    Since most modern filesystems, including Windows ones, allow whitespace in paths the answer is very likely yes.
    Goodie! That is a nice source for bugs or at least some sticky files! ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: PrintWriter not working, FileNotFound Exception!

    Curious as I am I did a little test on Windows 7:

    Java Code:
        public static void main(String[] args) {
    
            try{
                File test = new File("hi.txt ");
                PrintStream ps = new PrintStream(test);
                ps.print("hi");
                ps.close();
            } catch(Throwable t){
                t.printStackTrace();
            }
        }
    End result: a file "hi.txt" is created, no trailing space. So at least Microsoft knows to expect the work of inaccurate programmers.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by gimbal2 View Post
    Curious as I am I did a little test on Windows 7:

    End result: a file "hi.txt" is created, no trailing space. So at least Microsoft knows to expect the work of inaccurate programmers.
    What if I want a file with a space at the end?

    Tested on Fedora 19: Linux obeys its master and creates a file with the space at the end.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by SurfMan View Post
    What if I want a file with a space at the end?
    Then you probably also like to chastise yourself ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by SurfMan View Post
    What if I want a file with a space at the end?
    Do you really want an answer to that? Hint: It has nothing to do with the OS. ;)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: PrintWriter not working, FileNotFound Exception!

    Quote Originally Posted by jim829 View Post
    Do you really want an answer to that? Hint: It has nothing to do with the OS. ;)

    Regards,
    Jim
    What I am saying is, why does my OS decide/interfere on what filenames I can choose (besides the unusable characters <>:"/\|? and *)? If I want to shoot myself in the foot with some weird filename, then please let me. Why the hell is it being "autocorrected". You know what a good idea would be? This:

    PrintWriter not working, FileNotFound Exception!-looks-like-you-ve-added-space-end-t.png

    Edit: found this little gem:
    Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".
    Source: Naming Files, Paths, and Namespaces (Windows)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: PrintWriter not working, FileNotFound Exception!

    Because it's Windows - an OS designed for a crowd which needs a little more help. For control freaks like you who want to use a hammer to actually break their fingers "because they can and should be able" there is Linux :)

    Now I wonder what MacOS does actually!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. PrimeFaces pagination not working and throwing exception
    By rithish in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 11-04-2014, 08:50 AM
  2. FileNotFound Exception
    By ShadowWalker in forum New To Java
    Replies: 2
    Last Post: 05-20-2014, 03:28 PM
  3. Need help with PrintWriter!
    By PapaEcho in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 06:51 PM
  4. Exception Handling not working
    By collwill in forum New To Java
    Replies: 5
    Last Post: 05-02-2011, 05:37 AM
  5. Help with printwriter.
    By Addez in forum New To Java
    Replies: 2
    Last Post: 10-30-2009, 01:58 PM

Tags for this Thread

Posting Permissions

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