Results 1 to 7 of 7
- 01-22-2013, 06:51 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Why am I getting FileNotFoundException from this code
Hi,
I am working on a project where I am trying to save an object to a file. Back when I was in college it was working, but now, when I run the same code. I get a FileNotFoundException.
For instance this is the sample code:
File file = new File("c:/file.dat");
FileOutputStream oFile = new FileOutputStream(file);
if(!file.exists()) file.createNewFile();
ObjectOutputStream oOut = new ObjectOutputStream(oFile);
oOut.writeObject(someObject);
Some how I get a FileNotFoundException. I mean the whole idea of writing to the FileOutputStream is to create the file, right? But it is not working. Can anyone help me...Thanks.
Could it be because back in college I use java version 1.6, and now we have 1.7? Or is it still the same?
- 01-23-2013, 09:37 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Why am I getting FileNotFoundException from this code
What is the full exception (including stack trace), and highlight the line on which it occurs.
Please do not ask for code as refusal often offends.
- 01-23-2013, 10:41 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Re: Why am I getting FileNotFoundException from this code
Below is the stack trace:
java.io.FileNotFoundException: e:\eclipse\eclipse%20projects\MyProject\bin\sub\sa ved_games\game1.ccgf (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.ChessCraft.ChessBoard.ChessBoard.saveGame(Ches sBoard.java:7599)
at com.ChessCraft.ChessApp.Applet.ChessApplet.actionP erformed(ChessApplet.java:806)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Below is the highlighted line where the exception occurs:
File file = new File("c:/file.dat");
FileOutputStream oFile = new FileOutputStream(file);
if(!file.exists()) file.createNewFile();
ObjectOutputStream oOut = new ObjectOutputStream(oFile);
oOut.writeObject(someObject);
I hope you can see whats wrong, thanks for helping.
By the way, in this post there seems to be an error, where the path link says, sa ved_games, but it exactly, the cut and paste does not have the space, so that error is not made in the code...
- 01-23-2013, 10:54 PM #4
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Re: Why am I getting FileNotFoundException from this code
I found an error myself, but now I get an IOException:
Bellow is the code with the highlighted line where the exception is thrown:
try {
File sourceFile = new File(filePath);
System.out.println("Saving Game...");
if(!sourceFile.exists()) sourceFile.createNewFile();
FileOutputStream osFile = new FileOutputStream(sourceFile);
ObjectOutputStream oOutput = new ObjectOutputStream(osFile);
oOutput.writeObject(board);
oOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bellow is the stack trace:
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Nati ve Method)
at java.io.File.createNewFile(Unknown Source)
at com.ChessCraft.ChessBoard.ChessBoard.saveGame(Ches sBoard.java:7598)
at com.ChessCraft.ChessApp.Applet.ChessApplet.actionP erformed(ChessApplet.java:804)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 01-23-2013, 10:56 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Re: Why am I getting FileNotFoundException from this code
Oh by the way, in the stack trace, if you read anything that hints to a chess game...well this is that the project is, a chess video game...The game itself works fine, everything on it works fine, but when I am trying to save the file that I create for the game, it get that exception.
- 01-23-2013, 10:58 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Re: Why am I getting FileNotFoundException from this code
And I don't think it has to do with the object being Serializable, because I made sure of that first.
- 01-24-2013, 12:53 AM #7
Member
- Join Date
- Jan 2013
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Help | FileNotFoundException: db.properties
By Maxpain43 in forum NetBeansReplies: 0Last Post: 07-22-2011, 10:35 AM -
Help with FileNotFoundException
By Beginner in forum New To JavaReplies: 11Last Post: 11-22-2010, 04:44 PM -
FileNotFoundException
By ProgrammingPup in forum Advanced JavaReplies: 4Last Post: 12-30-2009, 01:29 AM -
filenotfoundexception :@
By wildheart in forum New To JavaReplies: 2Last Post: 04-25-2009, 09:56 AM -
FileNotFoundException
By PeonLover in forum New To JavaReplies: 1Last Post: 12-26-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks