0

I have a text file and it has -

packagename:com.hello

I have non-activity Java Class which has to read this text file fetch this com.hello and output it in the form of Log or Toast Message. I am doing Programming in Android in Eclipse. I have 2 questions..

1) Where do I need to place this text file I mean the location of it so that my JAva Class can read it.

2) Since my JAva Class is non-activity class, openFileInput is not working since it needs context and I have no way of getting context.

FileInputStream in = openFileInput("filename.txt");

Is there any way of doing it. Thanks in advance :)

FAZ
  • 255
  • 1
  • 3
  • 13

2 Answers2

1

1)You can place it anywhere inside your package ; just ensure you provide the correct path.
2)Refer this : How can I read a text file in Android?

Community
  • 1
  • 1
AnswerDroid
  • 1,873
  • 2
  • 33
  • 52
  • I am using Ubuntu and I am placing my file in /home/Myself/git/github/HelloApp/filename.txt and I am getting FileNotFoundException... HelloApp is my app name – FAZ Dec 16 '15 at 12:11
1

1) Where do I need to place this text file I mean the location of it so that my JAva Class can read it.

Anywhere you want, just tell your app the correct path to the file.

2) Since my JAva Class is non-activity class, openFileInput is not working since it needs context and I have no way of getting context.

Just read the file in Java!!!

FileInputStream fis = new FileInputStream(new File("path/to/your/file.txt"));

NOTES:

  • you must throw or catch a FileNotFoundException
  • remember closing the stream when finished!!
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I am using Ubuntu and I am giving it as FileInputStream fis = new FileInputStream(new File(" /home/Myself/git/github/HelloApp/filename.txt")); It is giving filenotfoundexception – FAZ Dec 16 '15 at 12:20
  • Then I need to put my text file in assets folder but getAssets() wont work for non-activity when I use AssetManager assetManager = getAssets(); – FAZ Dec 16 '15 at 12:37
  • absolutely **not**, don't care you have an Activity or not, `getAssets()` is simply a shortcut, but [**Android FileSystem**](http://stackoverflow.com/questions/2421826/what-is-androids-file-system) remains the same... But if you don't want to use straight, then get the asset statically from an Activity class, something like `UtilityActivity.getAsset(fileName);` – Jordi Castilla Dec 16 '15 at 12:43
  • Ok so I adb push my text file to '/data/local/' and then from Java Class, I use this FileInputStream fis = new FileInputStream(new File(" /data/local/filename.txt")); This should work ? – FAZ Dec 16 '15 at 12:52