So I have this class:
http://pastebin.com/EwXFwuZz
And this directory tree:
http://s14.directupload.net/file/d/3099/uskko5mo_png.htm
And I'm working with the LibGDX Framework on this project. This is basically my problem:
I have a file that contains level information in "chunks". Each line is one chunk. I want to read the file line per line. Unfortunately the built in FileHandling system of LibGDX doesn't support line by line reading so I thought to stick to the stock java one.
However I'm getting this "FileNotFound" Exception:
java.io.FileNotFoundException: ./assets/data/lvls/example.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at gemini.cute.game.xvii.database.LevelReader.<init>(LevelReader.java:49)
at gemini.cute.game.xvii.core.MainLauncher.create(MainLauncher.java:40)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:124)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:107)
With LibGDX the file is found but read into ONE single gigantic string. And for some reason with the same path (even going from the root) the file isn't found.
For people not familiar with LibGDX I'm coding in the upper "CuteGameXVII" project but for compilation I run the "Main" in "CuteGameXVII-desktop". The assets folders are linked via eclipse and worked for other resources so far.
Am I missing something super obvious here? If so, please help me :P Thank you in advance.
If you're running the Java program from a directory with path $DIR, the input file should be at $DIR/assets/data/lvls/example.txt. Based on the exception that you've received, the input file doesn't exist at this location.
I'd suggest that you first try using the absolute path to the input file in your code. Then, figure out what the relative path to it is.
I experienced this issue too. In order to read a file from your asset directory with LibDGX you must use the LibGDX method replacing:
new FileInputStream("SomeFile.txt")
by
Gdx.files.internal("SomeFile.txt").read()
assuming "someFile.txt" is in your asset root folder.
Related
I am trying to read the files inside a folder, but when I run the program it throws this exception. I tried with some other folders also. It throws the same exception.
Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
You cannot open and read a directory, use the isFile() and isDirectory() methods to distinguish between files and folders. You can get the contents of folders using the list() and listFiles() methods (for filenames and Files respectively) you can also specify a filter that selects a subset of files listed.
check the rsp's reply
check that you have permissions to read the file
check whether the file is not locked by other application. It is relevant mostly if you are on windows. for example I think that you can get the exception if you are trying to read the file while it is opened in notepad
Also, in some cases is important to check the target folder permissions. To give write permission for the user might be the solution. That worked for me.
Here's a gotcha that I just discovered - perhaps it might help someone else. If using windows the classes folder must not have encryption enabled! Tomcat doesn't seem to like that. Right click on the classes folder, select "Properties" and then click the "Advanced..." button. Make sure the "Encrypt contents to secure data" checkbox is cleared. Restart Tomcat.
It worked for me so here's hoping it helps someone else, too.
Check the file path properly, usually we mention the location and forget to specify the file name or the exact position where it belongs to.
This solution worked:
My JDK is installed in one directory(C drive) and
my java program as well as other files are in another directory(D drive).
Here's the solution from another Stack Overflow question
I have tried many variants but I cant find correct.
I have something like
Inside my jar, which created by Maven I can see that
That is my folder with classes. And, by the way, If I start my program in IDEA, not from Console, there is not any exception with paths
Here, I am in debug mode start my jar trying to see, where is the problem.
If I do 'file.exists()' it would be false but file inside. I think, that problem because of '.jar!\' in the path, but I don`t know how to remove that.
Anyway I've tried absolute and relative path, I've tried
Thread.getCurrentThread.getContextLoader.getResource()
GUI.class.getResource()
GUI.class.getClassLoader.getResource()
Nothing help
You can't use File to open resources inside a jar file. File can only be used with normal files and directories.
Note: using File works fine within in the IDE, since all files are not packaged in a jar file yet. But the program will break after you package it.
Once you locate the resource eg. URL res = GUI.class.getResource("/rxtx64/myres.dll") , you can open that resource as a stream InputStream is = res.openStream(); .
See also related answers Utils to read resource text file to String (Java) and How to read a text-file resource into Java unit test?
I made a small Java program for academic purposes, its main focus is to read some .txt files and present the information to the user. These files are present in the resources folder, under the src folder.
The program runs as intended when launched from Eclipse.
Using the Launch4j app I was able to successfully create an exe which runs fine and does what's intended, up until I try to read the .txt files I have in the resources folder, which appears not to be able to reach.
I'm guessing that when I launch the exe the run time path would change to where the exe was created, so I created the program in a desktop folder and specified this path in the program, but that doesn't seem to solve the situation.
As an alternative, I moved the .txt files out of the program and once again created the exe in a desktop folder with said .txt files, linked the program to this path and once again it didn't work.
The command used to get the .txt files is:
Files.readAllLines(Paths.get(doc)).get(line)
And doc is simply the path to the intended .txt file.
It's worth noting that I have no previous experience in Java and throughout the development of the program I tried my best to use commands I'd fully understand and to keep it as simple as possible. I hope the solution can be along these lines! I'm very confident this must be a rookie mistake, but I can't seem to find the solution to this specific problem anywhere.
The paths to files in Eclipse are different than the paths to files in an .exe or JAR file.
I will let this other user explain it because I am lazy :p
Rather than trying to address the resource as a File just ask the
ClassLoader to return an InputStream for the resource instead via
getResourceAsStream:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
As long as the file.txt resource is available on the classpath then
this approach will work the same way regardless of whether the
file.txt resource is in a classes/ directory or inside a jar.
The URI is not hierarchical occurs because the URI for a resource
within a jar file is going to look something like this:
file:/example.jar!/file.txt. You cannot read the entries within a jar
(a zip file) like it was a plain old File.
This is explained well by the answers to:
How do I read a resource file from a Java jar file?
Java Jar file: use resource errors: URI is not hierarchical
The original post is here, all credit to its author.
Fixing your URL should let you read from that file when you are using the .exe.
EDITED FOR CORRECTION. Thanks #VGR (see comments) for correcting my mistake.
So I have a problem with loading a file when running my program from a jar file. I am aware that questions very similar to this exist but I can find none that work for me or do what I need. I would like to load an object file from a folder in my jar but when I do I get the following error:
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at bbsource.BouncyBallV5.loadLevels(BouncyBallV5.java:370)
at bbsource.BouncyBallV5.<init>(BouncyBallV5.java:243)
at BBDriver.main(BBDriver.java:18)
Line 370 is as follows
initSource = new File(getClass().getResource("/resources/levels").toURI());
I have no problems with this line when running from Eclipse but I am aware that things act differently in jar files. The folder hierarchy is:
src
resources
levels
tier_one
Level1.cbbl
Level2.cbbl
tier_two
Level1.cbbl
levels is a directory that it will not let me access, and I'm not sure how to get it to work. I have seen suggestions such as using InputStream but I'm not sure how to use that and still treat it as a file from which I can read objects.
Any help would be appreciated.
This is because /resources/levels is a directory not a file. Check the answers for this question
I keep getting this error every time I run my OpenGL program and try and set up a texture:
Unable to read texture file: java.io.FileNotFoundException: bricks.gif (No such file or directory)
I have the file bricks.gif in the same folder and it shows up in Eclipse as well, but I have no idea what is going on. I tried to clean and refresh as well, but nothing.
Any help would be greatly appreciated.
Two ideas for solving your problem:
Make the reference to bricks.gif absolute, adding the full path.
(e.g. /Users/abc/workspace/bricks.gif)
When Eclipse starts your Application, the directory root will be your project folder.
Think of the following layout in Eclipse Package Explorer:
projectname
-src
-bin
-...
-bricks.gif
You can refer to the file in relative way by using the path "./bricks.gif". You can obtain a File Object pointing to bricks.gif with the following code:
File bricks = new File("./bricks.gif");
or
File bricks = new File("bricks.gif");
Cheers