java文件是在
应用程序/ SRC /主/爪哇/ nate.marxBros / QuoteBank.java
txt文件位于
应用程序/ src目录/主/资产/ Quotes.txt
代码是
- File file = new File("assets/QuotesMonkeyBusiness.txt");
- Scanner input = null;
- try {
- input = new Scanner(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
不应该像其他任何java程序一样工作吗?但它给文件找不到异常
我在这个网站上尝试过很多东西
Android Studio Reading from Raw Resource Text File
但该方法不起作用,因为我不知道如何传递上下文
谢谢你的帮助
更新的代码
- 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);
- }
主要活动
- public class MainActivity extends ActionBarActivity {
- QuoteBank b = new QuoteBank(MainActivity.this);
解决方法
在QuoteBank.java中设置一个私有变量:
- private Context mContext;
设置构造函数:
- public QuoteBank(Context context) {
- this.mContext = context;
- }
然后在你的活动中实例化它,
- QuoteBank quoteBank = new QuoteBank(context);
可以通过this命令或Activity.this在活动中调用上下文变量,其中将“Activity”替换为您的活动名称.或者,如果您在片段内,则可以从onCreateView(…)方法中的View对象获取上下文.通常通过调用view.getContext().
现在,在您抓取资产的方法中,您可以使用上下文:
- InputStream is = mContext.getAssets().open("QuotesMonkeyBusiness.txt")
既然您正在使用android studio,您可以创建一个main(String [] args){…}方法并运行它或者只是启动模拟器并让它使用Log.d(…)来显示来自文件.
或者,您也可以使用以下方法:
- AssetManager am = mContext.getAssets();
- InputStream is = am.open("QuotesMonkeyBusiness.txt");
将QuoteBank作为单例实例也可能有意义,这可能会提高效率,尽管这完全取决于您的要求,可能是这样的:
- List<String> allTextLines = QuoteBank.readFromFile(context,path_to_file);
然后在QuoteBank.java类中,您可以使用如下方法:
- /**
- * 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;
- }
}
然后在我的MainActivity.java类中,我有以下内容:
- /**
- * 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);
- }
- }
这是我的项目结构:
这是我的日志输出:
更新:为什么你不应该在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.
最后注意:
我强烈建议您阅读此处使用的所有对象的文档,以便您了解该过程.