Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Specifying the file location

  1. #1

    Default Specifying the file location

    I'm practicing a few problems off my book and also read a section of my book talking about how to specify the file location. One way would be
    PrintWriter outputFile=new PrintWriter("A:\\PriceList.txt");

    My problem occurs when I try to specify a file location as show below
    //Open an output file that appends data and does not delete it
    		FileWriter append_data=new FileWriter("C:\\hello.txt",true);
    		//Make a file that cvan print data into it
    		PrintWriter outputfile=new PrintWriter(append_data);

    Apparently, I believe this is not the right way to set my own path for the file...I'm just a novice in java so can anyone please explain to me how can I specify my file path.

    One more question would be that suppose I want to specify the file location on Desktop. What the format for that?

    Thanks in advance!


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Specifying the file location

    What error are you getting?

  3. #3

    Default Re: Specifying the file location

    This is the error thatis being specified to me on eclipse
    HTML Code:
    Exception in thread "main" java.io.FileNotFoundException: C:\hello.txt (Access is denied)
    	at java.io.FileOutputStream.openAppend(Native Method)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileOutputStream.<init>(Unknown Source)
    	at java.io.FileWriter.<init>(Unknown Source)
    	at demo.main(demo.java:11)

  4. #4
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Specifying the file location

    Specify a separate location for your output file e.g. C:\Users\[PC_NAME]\Desktop\hello.txt
    This should work on a Windows PC
    Who holds the KEY to all knowledge?

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Specifying the file location

    You may not have permission to write to a file in your root directory and may want to change it to a different directory. I am, of course, presuming you are working on a Windows platform.

Similar Threads

  1. Determining the location of the file that initiates an application
    By Avatar19 in forum File I/O & Other I/O Streams
    Replies: 23
    Last Post: December 10th, 2013, 09:22 AM
  2. Hardcode a file location into code, removing user input
    By PB94941 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2013, 06:09 AM
  3. SAX parser using wrong file location
    By aueddonline in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 8th, 2012, 10:56 AM
  4. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 07:59 AM
  5. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 12:56 AM