你应该有一个MainActivity.java或一些实例化QuoteBank的Activity.您希望构造函数接受上下文的参数:
在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 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 readLine(String path) {
List 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 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);
}
}
这是我的项目结构:
这是我从随机数据库下载的adventur.txt文件:
这是我的日志输出:
更新:为什么你不应该在Android中使用扫描仪
从官方文档:
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应用中使用Java读取资产目录下的TXT文件。通过上下文获取AssetManager,然后利用BufferedReader读取文件内容,将其存储到List中。文章提供了一个完整的QuoteBank类示例,包括构造函数、读取文件的方法,以及MainActivity中的应用示例,展示了如何在onCreate方法中读取文件并打印日志。

被折叠的 条评论
为什么被折叠?



