Reading from a text file in Android Studio Java

I have a QuoteBank class that needs to be read in a txt file with a scanner, but it gives me an exception not found by the file

The java file is located in the application / SRC / main / Java / nate.marxBros / QuoteBank.java

File

txt is located at app / src / home / assets / quotes.txt

the code

File file = new File("assets/QuotesMonkeyBusiness.txt"); Scanner input = null; try { input = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } 

Does it really work like any other Java program? but it gives a file exception not found

I tried a lot of things on this site, like Reading Android Studio from a Raw Resource text file, but this method does not work, because I do not know how to get into the context

thanks for any help

updated code

 public class QuoteBank { private ArrayList<ArrayList<QuoteBank>> bank; private Context mContext; private ArrayList<QuoteQuestion> monkeyBuisness; public QuoteBank(Context context){ mContext = context; InputStream is = null; try { is = mContext.getAssets().open("QuotesMonkeyBusiness.txt"); } catch (IOException e) { e.printStackTrace(); } ArrayList<QuoteQuestion> monkeyBuisness = parseFileToBank(is); } 

Mainactivity

 public class MainActivity extends ActionBarActivity { QuoteBank b = new QuoteBank(MainActivity.this); 
+5
source share
2 answers

You must have MainActivity.java or some Activity that creates an instance of QuoteBank . You want the constructor to accept the context parameter:

Set a private variable in QuoteBank.java :

 private Context mContext; 

Designer Setting:

 public QuoteBank(Context context) { this.mContext = context; } 

Then create an instance in your activity,

 QuoteBank quoteBank = new QuoteBank(context); 

A context variable can be called inside an action with this or Activity.this , where you replace "Activity" with your activity name. Alternatively, if you are inside a fragment, you can get the context from the View object inside your onCreateView(...) method. Usually by calling view.getContext() .

Now in your method, where you capture assets, you can use the context:

 InputStream is = mContext.getAssets().open("QuotesMonkeyBusiness.txt") 

Since you are using Android studio, you can either create the main(String[] args) { ... } method, or run it, or simply run the emulator and use Log.d(...) to output the output from the file.

Alternatively, you can also use the following method:

 AssetManager am = mContext.getAssets(); InputStream is = am.open("QuotesMonkeyBusiness.txt"); 

It may also make sense to have QuoteBank as a single-screen instance that can increase efficiency, although it all depends on your requirements, maybe something like:

 List<String> allTextLines = QuoteBank.readFromFile(context, path_to_file); 

And then in your QuoteBank.java class, you might have a method like this:

 /** * Created by AndyRoid on 5/23/15. */ public class QuoteBank { private Context mContext; public QuoteBank(Context context) { this.mContext = context; } public List<String> readLine(String path) { List<String> mLines = new ArrayList<>(); AssetManager am = mContext.getAssets(); try { InputStream is = am.open(path); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) mLines.add(line); } catch (IOException e) { e.printStackTrace(); } return mLines; } 

}

and then in my MainActivity.java class, I have the following:

 /** * Created by AndyRoid on 5/23/15. */ public class MainActivity extends AppCompatActivity { public static final String TAG = MainActivity.class.getSimpleName(); public static final String mPath = "adventur.txt"; private QuoteBank mQuoteBank; private List<String> mLines; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mQuoteBank = new QuoteBank(this); mLines = mQuoteBank.readLine(mPath); for (String string : mLines) Log.d(TAG, string); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 

This is my project structure:

enter image description here

This is the adventur.txt file downloaded from a random database:

file

This is my log output:

enter image description here


UPDATE: Why you should not use the scanner in Android

From the official documentation:

http://developer.android.com/reference/java/util/Scanner.html

This class is not as useful as it might seem. It is very inefficient for communication between machines; You should use JSON, protobufs or even XML for this. A very simple use might go away with split (String). To input information from people, using regular expressions specific to the locale makes it not only costly, but also somewhat unpredictable. The Scanner class is not thread safe.


FINAL NOTE:

I highly recommend that you read the documentation of all the objects used here so you can understand the process.

+4
source

Context.getResources().getAssets().open("QuotesMonkeyBusiness.txt"); returns an InputStream rest that you can take from there

Edit

because I don’t know how to go into context

Context actually exists, if you have a View , you can get a Context , if you Activity can get a Context to find it

Hope this helps

+1
source

All Articles