Results 1 to 7 of 7
  1. #1
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default 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...

  4. #4
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default 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)

  5. #5
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default 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.

  6. #6
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default 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.

  7. #7
    ElvisMachuca is offline Member
    Join Date
    Jan 2013
    Posts
    32
    Rep Power
    0

    Default Re: Why am I getting FileNotFoundException from this code

    Ah, never mind, I fixed the problem...it turns out that AudioClip is not serializable, so it did had to do with the what I said earlier...my bad, but now I am fine..saving the game and retrieving it works.

Similar Threads

  1. Help | FileNotFoundException: db.properties
    By Maxpain43 in forum NetBeans
    Replies: 0
    Last Post: 07-22-2011, 10:35 AM
  2. Help with FileNotFoundException
    By Beginner in forum New To Java
    Replies: 11
    Last Post: 11-22-2010, 04:44 PM
  3. FileNotFoundException
    By ProgrammingPup in forum Advanced Java
    Replies: 4
    Last Post: 12-30-2009, 01:29 AM
  4. filenotfoundexception :@
    By wildheart in forum New To Java
    Replies: 2
    Last Post: 04-25-2009, 09:56 AM
  5. FileNotFoundException
    By PeonLover in forum New To Java
    Replies: 1
    Last Post: 12-26-2007, 06:15 AM

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
  •