从Android Studio Java中的文本文件中读取

前端之家收集整理的这篇文章主要介绍了从Android Studio Java中的文本文件中读取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类QuoteBank需要读取扫描仪的txt文件,但它给我一个文件未找到异常

java文件是在
应用程序/ SRC /主/爪哇/ nate.marxBros / QuoteBank.java

txt文件位于
应用程序/ src目录/主/资产/ Quotes.txt

代码

  1. File file = new File("assets/QuotesMonkeyBusiness.txt");
  2. Scanner input = null;
  3. try {
  4. input = new Scanner(file);
  5. } catch (FileNotFoundException e) {
  6. e.printStackTrace();
  7. }

不应该像其他任何java程序一样工作吗?但它给文件找不到异常

我在这个网站上尝试过很多东西
Android Studio Reading from Raw Resource Text File
但该方法不起作用,因为我不知道如何传递上下文

谢谢你的帮助

更新的代码

  1. public class QuoteBank {
  2. private ArrayList<ArrayList<QuoteBank>> bank;
  3. private Context mContext;
  4. private ArrayList<QuoteQuestion> monkeyBuisness;
  5.  
  6.  
  7. public QuoteBank(Context context){
  8. mContext = context;
  9. InputStream is = null;
  10. try {
  11. is = mContext.getAssets().open("QuotesMonkeyBusiness.txt");
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15.  
  16. ArrayList<QuoteQuestion> monkeyBuisness = parseFileToBank(is);
  17. }

主要活动

  1. public class MainActivity extends ActionBarActivity {
  2. QuoteBank b = new QuoteBank(MainActivity.this);

解决方法

你应该有一个MainActivity.java或一些实例化QuoteBank的Activity.您希望构造函数接受上下文的参数:

在QuoteBank.java中设置一个私有变量:

  1. private Context mContext;

设置构造函数

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

然后在你的活动中实例化它,

  1. QuoteBank quoteBank = new QuoteBank(context);

可以通过this命令或Activity.this在活动中调用上下文变量,其中将“Activity”替换为您的活动名称.或者,如果您在片段内,则可以从onCreateView(…)方法中的View对象获取上下文.通常通过调用view.getContext().

现在,在您抓取资产的方法中,您可以使用上下文:

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

既然您正在使用android studio,您可以创建一个main(String [] args){…}方法并运行它或者只是启动模拟器并让它使用Log.d(…)来显示来自文件.

或者,您也可以使用以下方法

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

将QuoteBank作为单例实例也可能有意义,这可能会提高效率,尽管这完全取决于您的要求,可能是这样的:

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

然后在QuoteBank.java类中,您可以使用如下方法

  1. /**
  2. * Created by AndyRoid on 5/23/15.
  3. */
  4. public class QuoteBank {
  5.  
  6. private Context mContext;
  7.  
  8. public QuoteBank(Context context) {
  9. this.mContext = context;
  10. }
  11.  
  12. public List<String> readLine(String path) {
  13. List<String> mLines = new ArrayList<>();
  14.  
  15. AssetManager am = mContext.getAssets();
  16.  
  17. try {
  18. InputStream is = am.open(path);
  19. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  20. String line;
  21.  
  22. while ((line = reader.readLine()) != null)
  23. mLines.add(line);
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. return mLines;
  29. }

}

然后在我的MainActivity.java类中,我有以下内容

  1. /**
  2. * Created by AndyRoid on 5/23/15.
  3. */
  4. public class MainActivity extends AppCompatActivity {
  5.  
  6. public static final String TAG = MainActivity.class.getSimpleName();
  7.  
  8. public static final String mPath = "adventur.txt";
  9. private QuoteBank mQuoteBank;
  10. private List<String> mLines;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. mQuoteBank = new QuoteBank(this);
  18. mLines = mQuoteBank.readLine(mPath);
  19. for (String string : mLines)
  20. Log.d(TAG,string);
  21. }
  22.  
  23. @Override
  24. public boolean onCreateOptionsMenu(Menu menu) {
  25. // Inflate the menu; this adds items to the action bar if it is present.
  26. getMenuInflater().inflate(R.menu.menu_main,menu);
  27. return true;
  28. }
  29.  
  30. @Override
  31. public boolean onOptionsItemSelected(MenuItem item) {
  32. // Handle action bar item clicks here. The action bar will
  33. // automatically handle clicks on the Home/Up button,so long
  34. // as you specify a parent activity in AndroidManifest.xml.
  35. int id = item.getItemId();
  36.  
  37. //noinspection SimplifiableIfStatement
  38. if (id == R.id.action_settings) {
  39. return true;
  40. }
  41.  
  42. return super.onOptionsItemSelected(item);
  43. }
  44. }

这是我的项目结构:

这是我从随机数据库下载的adventur.txt文件

这是我的日志输出

更新:为什么你不应该在Android中使用扫描仪

从官方文档:

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

This class is not as useful as it might seem. It’s very inefficient for communicating between machines; you should use JSON,protobufs,or even XML for that. Very simple uses might get away with split(String). For input from humans,the use of locale-specific regular expressions make it not only expensive but also somewhat unpredictable.
The Scanner class is not thread-safe.

最后注意:

我强烈建议您阅读此处使用的所有对象的文档,以便您了解该过程.

猜你在找的Android相关文章