嗨我仍然是Android编程的新手。我想知道如何在android代码中读取像“/home/emulator/0/blabla/file.txt/”这样的文件路径。我需要知道如何读取文件路径,因为我必须从我将要制作的应用程序访问Android上的特定文件。并且路径已经在代码上定义,因此用户只能获得读数的输出。
public void naivebayes() throws Exception {
//im using weka to read a .arff file
//the problem is i didnt know how to "write" the path, from where i should start it
//p.s: i already tried open the files on my phone then clikc "Details" to see the path on the phone and write it on the code but the code still could'nt read the files
DataSource source = new DataSource("File Path");
Instances dataset = source.getDataSet();
//set class index to the last Attributes
dataset.setClassIndex(dataset.numAttributes()-1);
//create and build the classifier
NaiveBayes nb = new NaiveBayes();
nb.buildClassifier(dataset);
tv.setText(nb.getCapabilities().toString());
}
答案 0 :(得分:1)
不要使用硬编码文件路径。该框架将为您提供要将文件保存到的区域的基本路径。
对于SD卡,请使用Environment.getExternalStorageDirectory()
对于本地文件,请使用Context.getFilesDir()
(或Context.openFileOutput(String name, int mode)
等)
对于本地缓存,请使用Context.getCacheDir()