• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner can't find file to read from. Eclipse.

 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attached two pictures that will help explain the problem. The Scanner in class RowReader can't find a file with this path: "/dict/en_US2sv_SV.dict".
The dict-folder is found at the bottom of the package-explorer. Maybe I should mention that this is an Android-project and I do not want to access the files from the assets-folder if it is possible to avoid.
Any ideas?

Screenshot from 2014-05-28 19:36:36.png
[Thumbnail for Screenshot from 2014-05-28 19:36:36.png]
RowReader
Screenshot from 2014-05-28 19:32:25.png
[Thumbnail for Screenshot from 2014-05-28 19:32:25.png]
Log
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try creating a File object for this path and print out the value returned from calling its getAbsolutePath() method. Is this value the same as the absolute path to the file on the file system?
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Try creating a File object for this path and print out the value returned from calling its getAbsolutePath() method. Is this value the same as the absolute path to the file on the file system?


If I understood correctly: File file = new File("/dict/en_US2sv_SV.dict"); --> file.getAbsolutePath() --> "/dict/en_US2sv_SV.dict". Yes it's the same.

I've tried to refresh, import, drag and drop but nothing works. The dict-folder is on the same level as the source-folder. Maybe it has something to do with it being an Android-project?

Log.
 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:The dict-folder is on the same level as the source-folder.


But your code looks for it in the root directory. Remember, just because you see your code as an "Android project", that doesn't change how the operating system sees your file. If it starts with "/" then it starts looking for it in the root directory, not in your source directory.
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Sven Sylta wrote:The dict-folder is on the same level as the source-folder.


But your code looks for it in the root directory. Remember, just because you see your code as an "Android project", that doesn't change how the operating system sees your file. If it starts with "/" then it starts looking for it in the root directory, not in your source directory.


I have tried without the "/" at the beginning but that didn't work either.

Just to clearify: In my project-folder I have the source folder and a dict-folder. The dict-folder contains the file i want to read. The dict-folder is not in the source-folder, maybe it should be(but I have tried this as well)?
 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:I have tried without the "/" at the beginning but that didn't work either.


Because now you're using a path which is relative to the current working directory, which presumably isn't the same as your source directory.

If you're writing an Android application then you won't want either of those options -- relative to the root directory or relative to your source directory (which won't exist when you run the code on Android). I know nothing about Android applications but presumably there's a way to identify a file as being part of the application, like maybe a method you call which you pass a path to. But that's an Android question, not a Beginning Java question. Since I'm not sure what your intentions are with respect to this file I'm not going to move the thread to the Android forum, but if you ask for that to be done then a moderator will do it for you.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To solve you can either go up and out of the src directory then down into the dict directory or specify the absolute path from the root of the file system

Example:

Seeing that the RowReader class is within src/com/example/dict and the dict directory is relative to the src directory to access the file in that directory your path will have to be specified as ../../../../dict/en_US2sv_SV.dict or you can specify the absolute path as say /home/sven/workspace/Dictionary/dict/en_US2sv_SV.dict
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:To solve you can either go up and out of the src directory then down into the dict directory or specify the absolute path from the root of the file system

Example:

Seeing that the RowReader class is within src/com/example/dict and the dict directory is relative to the src directory to access the file in that directory your path will have to be specified as ../../../../dict/en_US2sv_SV.dict or you can specify the absolute path as say /home/sven/workspace/Dictionary/dict/en_US2sv_SV.dict


I've tried both these suggestions but none of them worked so it would be great if the moderator could move this thread to the android forum? Thanks for your help.
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it even possible to read from a file the "ordinary/usual" way in an Android/Eclipse project? I want to do it without putting the files in the assets-folder and without using something like InputStream iS = resources.getAssets().open(fileName); ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:Is it even possible to read from a file the "ordinary/usual" way in an Android/Eclipse project?

Ignore Eclipse, it has nothing to do with it except as an IDE. You need to concentrate on the Android side of things. Yes, Android does let you access the file system in the 'ordinary' way, but the paths you have access to are not as expected.

1) If you want the file to be delivered with your application, then it has to be packed in the APK. Only certain paths are packed in the APK, it is not an arbitrary file structure, so the file should go in the assets or res/raw folder
2) If you download the file then you need to store it in a path where you can find and access it. This is usually in the application's Data path or the SDCard. See this for how to get references to your app's directories: http://developer.android.com/guide/topics/data/data-storage.html

I want to do it without putting the files in the assets-folder and without using something like InputStream iS = resources.getAssets().open(fileName); ?

Writing off a means to do something is almost always a red flag. Why don't you want to do it the easiest way?
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
Writing off a means to do something is almost always a red flag. Why don't you want to do it the easiest way?


Because I wanted to avoid passing Context or / ActivityMain.this( not sure what to pass exactly) through several classes and packages just to get a reference in order to read from the file. But as far as I understand, I have to.

I have now put the file in the /res/raw/ directory and I also had to change the file-name to lower-case-letters. "en_us2sv_sv.dict"

I need to find the file with the File-class. Something like File file = context.something(???); Is this possible?

I need to use File because later I must invoke toURI();

Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:
Steve Luke wrote:
Writing off a means to do something is almost always a red flag. Why don't you want to do it the easiest way?


Because I wanted to avoid passing Context or / ActivityMain.this( not sure what to pass exactly) through several classes and packages just to get a reference in order to read from the file. But as far as I understand, I have to.

I have now put the file in the /res/raw/ directory and I also had to change the file-name to lower-case-letters. "en_us2sv_sv.dict"

I need to find the file with the File-class. Something like File file = context.something(???); Is this possible?

I need to use File because later I must invoke toURI();

Thanks.

If it is in the res/raw directory you need to use to use the resource locator: InputStream input = context.getResources().openRawResource(R.raw.en_us2sv_sv); If you want to access the file by String name and organize by folder then you would use the assets folder and access it via the AssetsManager: InputStream input = context.getAssets().open("dict/en_us2sv_sv.dict");
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will get back to you about URI. Half of it is 'why toURI() is needed'... but lack of time now for details.
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Will get back to you about URI. Half of it is 'why toURI() is needed'... but lack of time now for details.


I have been given a code-sceleton which I'm supposed to fill in. And the "row-reader"-class uses URI but I guess I could write my own "row-reader".

If I get the correct path then the File-object should work?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up: There is no File. The 'file' is packed inside the .apk (which is a specialized JAR/ZIP file). You have read-access to the Streams for the data only, no file manipulation options. This is the same for both the assets and the res/raw folder (the difference only being how you locate the 'file').

If you need access to the File for interacting with a library, you could put the file into the APK using the assets folder, then on your first access for a particular file*, copy it out of the APK to the application's data path (using context.getFilesDir() to get the base path to save to). That would give you a File which you could manipulate/get path to as needed.



*Or some other friendly/optimized way - you might be able to do it on first use, or when some configuration was set... etc, depending on use-case).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic