Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  File Not Found Exception  (Read 1661 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-04-15 23:54:33 »

I've been trying to get my options menu working for the past few hours now and it's just about finished; my only problem now is that the program, for some ridiculous reason, thinks that a file, that exists, doesn't exist.

Here is where I'm loading the file:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public String[] loadFile(String filePath)
    {
        int lineCounter = 0;
        ArrayList<String> lines = new ArrayList<>();
       
        try
        {
            final URL url = new URL(getClass().getProtectionDomain().getCodeSource().getLocation().toString()+filePath);
            System.out.println(url);
            final BufferedReader bufferedReader = new BufferedReader(new FileReader(url.toString()));
           
            String line = null;
           
            while((line = bufferedReader.readLine()) != null)
            {
                lines.add(line);
               
                lineCounter++;
            }

            bufferedReader.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       
        return lines.toArray(new String[lines.size()]);
    }


Here is the error message:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
java.io.FileNotFoundException: file:\C:\Users\Tyler\Java%20Projects\Text%20Based%20Game\build\classes\Options.txt (The filename, directory name, or volume label syntax is incorrect)
   at java.io.FileInputStream.open(Native Method)
   at java.io.FileInputStream.<init>(FileInputStream.java:138)
   at java.io.FileInputStream.<init>(FileInputStream.java:97)
   at java.io.FileReader.<init>(FileReader.java:58)
   at Core.ResourceHandler.loadFile(ResourceHandler.java:69)
   at Screens.ScreenMainMenu.<init>(ScreenMainMenu.java:25)
   at Core.GameCanvas.<init>(GameCanvas.java:37)
   at Core.GameFrame.<init>(GameFrame.java:21)
   at Core.GameFrame.main(GameFrame.java:46)


I've triple checked to make sure that the file does exist and that it has all of the needed information within, the information is a single integer on line 1 that say which music track to use. I've also tried a few different ways to point the program to the file but they all have the same result.

The way that I'm attempting to get it to work is that, no matter where the .jar file is, there will always be an Options.txt in the same folder that will contain all of the options for the program; this will be read from when the program starts up and then written to if anything is changed in the menu. Everything looks like it should work but the program is just keeps throwing a file not found exception...
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-04-16 02:37:46 »

Try using an absolute path (i.e. "C:\Users\Username\blah\blahblah\options.txt"). If that doesn't work, then there's something wrong with
1  
getClass().getProtectionDomain().getCodeSource().getLocation().toString()+filePath
.
If that works, uh, could it be that you're not refreshing the project? I doubt it, seem to remember you using netbeans (auto refresh ftw).
What is your argument for the loadFile method (String filePath, what are you setting filePath as)?
Online HeroesGraveDev

JGO Kernel


Medals: 292
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2013-04-16 03:55:28 »

What does
System.out.println(url);
give you?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-04-16 15:31:06 »

Sorry for the late reply, I went to sleep right after making this thread.

@Jimmt
filePath is being set as "Options.txt".

@HeroesGraveDev
Printing out the url gives me "file:/C:/Users/Tyler/Java%20Projects/Text%20Based%20Game/build/classes/Options.txt", without the quotation marks.
This error is really weird because it's pointing to the proper file yet it doesn't think the file is there even though it is...
Offline StrideColossus
« Reply #4 - Posted 2013-04-16 15:45:04 »

If the file is on your class-path (which it seems to be) then an alternative could be this:

1  
... new BufferedReader( new InputStreamReader( YourClass.class.getResourceAsStream( "/Options.txt" ) ) );


Online SHC
« Reply #5 - Posted 2013-04-16 15:49:52 »

Try this.

1  
final URL url = getClass().getClassLoader().getResource(filePath);


This works if the file is in the classpath. Or else, the file isn't in the classpath.

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-04-16 16:01:20 »

Sorry for the late reply, I went to sleep right after making this thread.

@Jimmt
filePath is being set as "Options.txt".

@HeroesGraveDev
Printing out the url gives me "file:/C:/Users/Tyler/Java%20Projects/Text%20Based%20Game/build/classes/Options.txt", without the quotation marks.
This error is really weird because it's pointing to the proper file yet it doesn't think the file is there even though it is...
I don't know the answer to this, try SHC or someone else's answer, but it's pointing to the path you fed it, not the actual existing file.
Offline tyeeeee1
« Reply #7 - Posted 2013-04-16 16:15:57 »

@SHC
Your suggestion doesn't work. When printing out the url it comes out as null and then there is a null pointer exception because of that.

@StrideColossus
Your suggestion also gives a null pointer exception just as SHC's did.


I'm not sure what a classpath is but the Options.txt file is supposed to be  located outside of the .jar file, when I've finished the program and turned it into a .jar, so that it can be read from and written to.
Offline Stranger

Senior Devvie


Medals: 7



« Reply #8 - Posted 2013-04-16 16:41:53 »

look at accepted answer of:

http://stackoverflow.com/questions/10830460/java-io-filenotfoundexception-file-not-found-using-scanner-whats-wrong-in-my

Anton
Offline tyeeeee1
« Reply #9 - Posted 2013-04-17 16:12:11 »


Just tried doing it that way as well but I'm still getting a NullPonterException.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
public String[] loadFile(String filePath)
    {
        int lineCounter = 0;
        ArrayList<String> lines = new ArrayList<>();
       
        try
        {
            URL url = ClassLoader.getSystemResource(filePath);
            final BufferedReader bufferedReader = new BufferedReader(new FileReader(url.toString()));
           
            String line = null;
           
            while((line = bufferedReader.readLine()) != null)
            {
                lines.add(line);
               
                lineCounter++;
            }

            bufferedReader.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       
        return lines.toArray(new String[lines.size()]);
    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #10 - Posted 2013-04-17 18:55:39 »

java.io.FileReader doesn't take URLs, or string representations of URLs, as parameters to its constructor; it expects a (local) file path.  When you pass a string with "%20" embedded in it to represent a space, the class will assume you mean literally "%20", and not a space character.  Hence the file isn't found, because what you passed to it literally does not exist.

You need to pass the file name "normally," not URL-encoded.  Or use some other construct, such as

1  
new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream("/path/to/resource")));

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #11 - Posted 2013-04-17 22:02:44 »

Are you just trying to read in a .txt file? Wouldn't it be easier to just use Scanner.

Another problem could be that your file isn't being placed in the right spot. In Eclipse or Netbeans, I think that files within the "build" folder are completely ignored and can't be read by the program. If I were you, I'd try putting it in a folder called "data" or something completely different.

Actually, the latter solution might be better to try than the former.

Offline brollysan

Junior Devvie


Medals: 1



« Reply #12 - Posted 2013-04-17 22:15:02 »

Are you sure you have the correct permission for that file? Have you tried reading it as a stream (seems you have) and not as a file? If it is in your classpath (try putting it in the same folder as the class the reader is being called from. If the structure is like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
-SomePackageInClassPath
  -SomeReaderClass.java
 OR
  -SomeOtherPackageOrFolder
    -SomeOtherPackageOrFolder
      ..etc
       -SomeReaderClass.java
-SomeFolder_or_SomePackage1
  -SomeFolder_or_SomePackage2
    -SomeFile.txt


Then the following in
1  
SomeReaderClass.java
will work inside .jar or in eclipse.
1  
2  
3  
4  
5  
6  
public BufferedReader readFileAsBuffer(String fileName) {
      BufferedReader reader = null;
      reader = new BufferedReader(new InputStreamReader(getClass()
            .getResourceAsStream(fileName)));
      return reader;
   }

1  
readFileAsBuffer("/SomeFolderOrPackage1/SomeFile.txt");


If the file is inside SomeFolderOrPackage1 and not under SomeFolderOrPackage2 then the following
1  
readFileAsBuffer("/SomeFile.txt");

Should work.
Offline tyeeeee1
« Reply #13 - Posted 2013-04-18 00:01:14 »

Are you just trying to read in a .txt file? Wouldn't it be easier to just use Scanner.

Another problem could be that your file isn't being placed in the right spot. In Eclipse or Netbeans, I think that files within the "build" folder are completely ignored and can't be read by the program. If I were you, I'd try putting it in a folder called "data" or something completely different.

Actually, the latter solution might be better to try than the former.

Yeah, that's all I'm trying to do --- Looks like I've completely over complicated this...

Edit: Just implemented a scanner and now my file loader is working perfectly.

Thanks for the help everyone, I can't believe how easy this was to solve; at least I won't need to do this again now that I know how to do it!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!