public void loadFile(int level){
try {
//Create new file
levelFile = new File("assets/levels.txt");
fis = new FileInputStream(levelFile);
isr = new InputStreamReader(fis);
reader = new BufferedReader(isr);
//Code to read the file goes here
Using this code, however, I keep getting the above error (java.io.FileNotFoundException).
The file definitely exists in my Assets folder and has the correct name. I've found a couple of similar questions on here and have tried various things including refreshing the project, cleaning the project, using "levels.txt" instead of "assets/levels.txt" but I keep getting this error.
Any ideas why?
Because you're dealing with outside the package, getResource() will be the best solution for your problem:
URL url = getClass().getResource("/assets/levels.txt");
File f = new File(url.toURI());
//....
Or you can directly get the input stream using getResourceAsStream() method :
InputStream is= getClass().getResourceAsStream("/assets/levels.txt");
isr = new InputStreamReader(is);
It's better since you don't have to use FileInputStream.
Note that URISyntaxException must be caught with FileNotFoundException or declared to be thrown.
In an Android project, the right way to read the content of asset files is by using the AssetManager. Asset files are the files you put in the assets/ folder of your Android project. This is mentioned briefly in the sidebar on the Accessing Resources page in the Android docs.
In particular, you can open the file assets/levels.txt and create a BufferedReader like this:
InputStream stream = context.getAssets().open("levels.txt");
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
Notice that the argument of the open call is simply levels.txt, not assets/levels.txt.
For more details see the full docs of AssetManager.
Related
I need to make a reference to a .txt file, but I don't know what the URL to the file is? I have made an assets folder, because its not auto-generated in Android Studio, and placed my file there.
File file = new File(URL)
What is the URL to my file?
For instance:
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
Check out AssetManager.
This is a chunk of data I'd like to access by a method.
I'm doing the following to read my file:
String fileName = "file.txt"
InputStream inputStream = new FileInputStream(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
My file.txt is in the same package, but I still get FileNotFoundException.
I didn't use a path url to point to the file because I thought since this it going to be an android application, hard-coding the path might not work when deployed... Please correct me if I am wrong. Thanks bunch!
This shows how to do that. https://stackoverflow.com/a/14377185/2801237
Also the 'package' your class is in has nothing to do with the 'path' where the file is being executed from. (two different concepts, 'package' = folder hierarchy of java source code files), 'path' = location on a filesystem of a specific file, your APK is being 'executed' in a particular place, and the location it writes a file is associated with that (I actually don't know where 'offhand' it writes by default, because I always get cache dir, or sd card root, etc.)
You may use:
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
I am trying to load a file that is within a jar file. I try to get the file to load in a BufferedReader. For example:
BufferedReader br = new BufferedReader(new FileReader(fileName));
where fileName is my string from the root of the Jar file: something like this "resources/text.txt"
I am having a hard time finding out how to make this happen. Obviously FileReader will not work since it reads from the file system.
Anyone that can help me out?
Use the classloader to get the resource as a stream.
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getClassLoader().getResourceAsStream("/resources/text.txt"), "utf-8");
Note that you need to specific the correct character encoding for the content.
If you are trying to access a file within the same jar as your running program you should use
final InputStream inputStream = ClassName.class.getResourceAsStream(fileName);
I have several custom assets like csv files. Where should i put these to make sure they will be found for each build/project (Desktop/IOS/Android)? I already tried the android/assets folder but the file is not found by the BufferedReader. Should i just keep my csv files there and navigate to that folder with the BufferedReader or is this incompatible between the different projects? Bonus points for including a proper way to navigate to correct map, i can look this up but worry about compatibility between projects.
I tried giving a LibGDX FileHandle as a paremeter to the BufferedReader without results. It just does not accept a LibGDX filehandle.
FileHandle handle = Gdx.files.internal("roomsets.csv");
br = new BufferedReader(new FileReader(handle)); //The constructor FileReader(FileHandle) is undefined
br = new BufferedReader(new InputStreamReader(handle).read()); //error: The constructor BufferedReader(int) is undefined
br = new BufferedReader(handle); //The constructor BufferedReader(FileHandle) is undefined
I fixed it with this:
br = new BufferedReader(handle.reader());
Just curious now if this should would bring up issues on IOS and Android?
I'm a beginner in Tomcat and Servlet.I come across a problem about reading File in servlet. I have search a lot information about this problem in stackoverflow,but I haven't solved it.I hope get some help.I write the code as following:
URL url=getServletContext().getResource("/WEB-INF/DataSpecification.owl");
File file=new File(url.toString());
FileInputStream input=new FileInputStream(file);
Reader in = new InputStreamReader(input,"UTF-8");
I get the following error:
java.io.FileNotFoundException: jndi:\localhost\MAGS\WEB-INF\DataSpecification.owl (File name or directory name wrong).
at java.io.FileInputStream.open(Native Method)
I put my file in WEB-INF directory.
I know that I can get an InputStream by using
getServletContext().getResourceAsStream()
But for some reasons,I nead to get a FileInputSream.
I hope to get your help!thank you!
This should work (no need to use FileInputStream):
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/DataSpecification.owl");
Reader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));