使用Android SDK从文件中读取一行
我已经试了三天了,我无法让我的程序从这个或任何其他文件中读取。请帮忙!我已经尝试创建另一个项目,并把这也没有工作使用Android SDK从文件中读取一行,android,file,Android,File,我已经试了三天了,我无法让我的程序从这个或任何其他文件中读取。请帮忙!我已经尝试创建另一个项目,并把这也没有工作 TextView orangeTitle; String line; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gameplay
TextView orangeTitle;
String line;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gameplay);
// Show the Up button in the action bar.
setupActionBar();
try {
cardReader(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setOrange();
}
public String cardReader(String args[]) throws IOException
{
FileInputStream fis = new FileInputStream("C:\\Users\\Alex\\Documents\\Android Development\\HumorMe\\assets\\orangecards.txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));
line = bfr.readLine();
bfr.close();
return line;
}
public void setOrange()
{
orangeTitle = (TextView)findViewById(R.id.textView1);
orangeTitle.setText(line);
}
看起来您正在尝试从计算机读取文件,这是不可能的,因为您的代码运行在单独的Android设备上。尝试在设备的SD卡上放置一个文本文件,并运行如下操作:
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()+"/test.txt");
您无法通过电脑上的资产路径访问资产。有关如何访问资产文件的信息,请参阅和。例如:
InputStream is = getAssets().open("orangecards.txt")
您正在读取存储在windows分区中的文件。你想做什么?哇,真不敢相信我花了那么多时间,这是一个简单的解决方案。不过这很好用!谢谢你的帮助,尼尔!