Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Writing to file within another package

801440Sep 19 2009 — edited Sep 19 2009
Hi all, I had a search in the forums, but I couldn't find what I was looking for.
I have what I consider to be good code organised into packages that make sense, but for some reason, I cannot programmatically save Files to where I want to.
If I create them manually, they read fine, (with
File xmlFile = new File(this.getClass().getClassLoader().
        getResource("data/records.xml").getFile());
SAXBuilder builder = new SAXBuilder();
Document xml = builder.build(xmlFile);
).
So now I want to create this file programatically.
The code I have is
// Make a new file
File xmlFile = new File("data/" + p.getName() + "deck.xml");
xmlFile.createNewFile();
But that gives (probably unsuprisingly)
java.io.IOException: The system cannot find the path specified
	at java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.io.File.createNewFile(File.java:883)
	at gamepackage.PlayerDeck.updateDeck(PlayerDeck.java:99)
When I create a file in the default package (probably frowned upon) with
// Try create file in default package
File xmlFile = new File(p.getName() + "deck.xml");
xmlFile.createNewFile();
It works as expected (except my file is in the default package).
So, it must be a package problem. I have tried messing around with mkdir(), but that creates Files in directories called "data" rather than my existing package "data" (which confuses my little soul, because I thought packages were treated as directories (see JAR files etc)), and I have tried messing around with URIs and URLs, to little avail. Also, ClassLoaders such as
File xmlFile = new File(this.getClass().getClassLoader().
	        	getResource("data/" + p.getName() + "deck.xml").getFile());
wont work, because I need to create the file before I can call .getFile(); but I need the File to call createNewFile() on.
Please can you point me in the right direction? Many thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 17 2009
Added on Sep 19 2009
5 comments
150 views