0

I have my classes in /src/com.example.myapp/ and I have a text mytext.txt there too.

However, when I reference static File f = new File("mytext.txt")); it does not find it, even though the file is in the same directory as the class.

What do I need to do? What directory is it actually looking in?

Assets is read-only. I need somewhere where I can read and update the text file.

user83039
  • 3,047
  • 5
  • 19
  • 31
  • 1
    Are you sure your classes are in /src/com.example.myapp/ and not in /src/com/example/myapp/ – Juned Ahsan Nov 13 '14 at 05:36
  • Yes, you're right. However, Eclipse shows it with periods. I wasn't sure what to put. – user83039 Nov 13 '14 at 05:37
  • possible duplicate of [android development open file txt and return content](http://stackoverflow.com/questions/11206082/android-development-open-file-txt-and-return-content) – Andrew T. Nov 13 '14 at 06:01
  • No it's not. How did you decide that? That one is not even using `File`... – user83039 Nov 13 '14 at 06:03
  • If you want writable file, [put in in Internal memory of device](http://developer.android.com/training/basics/data-storage/files.html) – SweetWisher ツ Nov 15 '14 at 06:41

3 Answers3

1

Use an assets folder.

Here is an example...

Loading array from a text file in assets folder (Android)

You create the assets folder in your root project folder then place your file in it. Once it's there, you access this way:

getAssets().open("file.txt");

the getAssets method is part of your Activity / Context. Context carriers a lot of the information about your app.

If you are not in an Activity, you can pass the Context to your class and use this:

context.getAssets().open("file.txt");
Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

Use the assets directory:

assets/

This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

Community
  • 1
  • 1
ipavl
  • 828
  • 11
  • 20
  • How do I reference it? `/appPackage/assets/mytext.txt`? – user83039 Nov 13 '14 at 05:38
  • 1
    You should be able to use `getAssets().open("mytext.txt")`. See here: http://stackoverflow.com/a/11206396/4088425 – ipavl Nov 13 '14 at 05:43
  • Reading the link see nothing about: Does `getAssets()` have to be imported? – user83039 Nov 13 '14 at 05:45
  • You might need to import `android.content.res.AssetManager`. Here's the reference doc: http://developer.android.com/reference/android/content/res/AssetManager.html – ipavl Nov 13 '14 at 05:49
  • Tried `import android.content.res.AssetManager;` and `import android.content.res.AssetManager.*;` – user83039 Nov 13 '14 at 05:57
  • 1
    If you're not in an activity, then you'll need to get a reference to the activity. You could pass a [`Context`](http://developer.android.com/reference/android/content/Context.html), which has the [`getAssets()`](http://developer.android.com/reference/android/content/Context.html#getAssets%28%29) method, in when you call the function. – ipavl Nov 13 '14 at 06:04
  • Oh, this is read-only. I need to update this file. http://stackoverflow.com/questions/10562904/is-asset-folder-read-only – user83039 Nov 15 '14 at 06:36
  • Depending on what you're doing, [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) may work. Otherwise you could try @SweetWisher's solution. – ipavl Nov 15 '14 at 15:16
0

If you want the file with EDIT mode, you can use Internal/External Storage

Then you can read it as:

String filePath = context.getFilesDir().getAbsolutePath();  //returns current directory.
File file = new File(filePath, fileName);
try {
    BufferedReader br = new BufferedReader(new FileReader(file));   
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); //the output text from file.

You can even write to this file :

String filename = "myfile";
String string = "ur data";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

Hope it will help you ツ

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74