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 8 of 8

Thread: How to specify FileOutputStream location?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default How to specify FileOutputStream location?

    So I'm using the following code below to serialize my arraylist of obejcts. it is work except that I want to be able to specify where it outputs the file.

    I thought I could change:
    FileOutputStream fout = new FileOutputStream("AccList.dat");
    to something like:
    FileOutputStream fout = new FileOutputStream("C:\Program Files\test\AccList.dat");

    but then it just crashes. I kind of understand that its just declaring FileOutputStream as AccList.dat I think... But how do I specify a save location?

    says:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )


    public void SaveArray()
    	{
    		/* serialize arraylist */
    		try
    		{
    			System.out.println("serializing list");
    			FileOutputStream fout = new FileOutputStream("AccList.dat");
    			ObjectOutputStream oos = new ObjectOutputStream(fout);
    			oos.writeObject(AAL.accArray);
    			oos.close();
    		} catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}// end SaveArray


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to specify FileOutputStream location?

    It's because the backslash is a special character in Java string land. You will need to escape it or turn it into a forwardslash.

    FileOutputStream fout = new FileOutputStream("C:\\Program Files\\test\\AccList.dat");
    OR

    FileOutputStream fout = new FileOutputStream("C:/Program Files/test/AccList.dat");
    That should do the trick.

  3. The Following User Says Thank You to Json For This Useful Post:

    Hallowed (May 25th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to specify FileOutputStream location?

    You have been so helpful! Thank you!

    Now.. Any chance you have an ftp server I can host a read/write AccList.dat file? haha.
    I want to see if I can get my program to save over internet. =P
    Last edited by Hallowed; May 25th, 2011 at 11:49 PM.

  5. #4
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to specify FileOutputStream location?

    Do you know why I wouldn't be able to load a picture from the internet for example: "http://i52.tinypic.com/307rt36.jpg"
    as an icon for a button? It just acts like there isn't one at all.
    well I guess not really cause it crashes if there is nothing there. but still?

    Icon sword1 = new ImageIcon ( ("http://i52.tinypic.com/307rt36.jpg"));

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to specify FileOutputStream location?

    Do you know why I wouldn't be able to load a picture from the internet
    See the API for ImageIcon, it contains a constructor which accepts a URL object that you can use to specify the location
    try{
        Icon sword1 = new ImageIcon ( new URL("http://i52.tinypic.com/307rt36.jpg") );
    }catch(MalformedURLException e){
        e.printStackTrace();
    }

  7. The Following User Says Thank You to copeg For This Useful Post:

    Hallowed (May 27th, 2011)

  8. #6
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to specify FileOutputStream location?

    so I imported these:
    import java.net.MalformedURLException;
    import java.net.URL;
    and
    I put this into my code:
    try
    		{
    			Icon sword2 = new ImageIcon(new URL(
    					"http://i52.tinypic.com/307rt36.jpg"));
    		} catch (MalformedURLException e)
    		{
    			e.printStackTrace();
    		}

    But now this wont work.



    fancyJButton.setRolloverIcon(sword2); // set rollover image
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    sword2 cannot be resolved to a variable

    at JTabbedPaneFrame.<init>(JTabbedPaneFrame.java:41)
    at JTabbedPaneDemo.main(JTabbedPaneDemo.java:9)


    I tried looking at the api before too but it didn't work for me when i tried it.
    I don't think I was doing it right cause all it said is:
    ImageIcon(URL location)

  9. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to specify FileOutputStream location?

    In the code you posted, the Icon 'sword2' is declared as a local variable inside the 'try' scope of a 'try..catch'. This means it is local to that block and disappears at the end, i.e. when the closing curly brace } is reached. If you want it to be accessible to other code, either put that other code inside the 'try' scope, i.e. after the 'sword2' declaration (this is how 'try...catch' is generally used), or move the declaration (but not the initialization) of 'sword2' outside the 'try' scope so it is in the scope of the code that wants to use it.

  10. The Following User Says Thank You to dlorde For This Useful Post:

    Hallowed (May 29th, 2011)

  11. #8
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to specify FileOutputStream location?

    Thank you. Totally forgot about that for some reason. =/ I'll try it out.
    Works! =]
    Last edited by Hallowed; May 29th, 2011 at 06:38 PM.

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. JFileChoose and FileOutputStream, nothing is being saved
    By lost in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 27th, 2010, 08:10 PM
  3. 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
  4. [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
  5. setting location of a dialog relative to the parent
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2009, 05:42 PM