2016-10-13 37 views
-1

嗨im仍然是新手在android编程。我想知道如何在android代码中读取像“/home/emulator/0/blabla/file.txt/”这样的文件路径。我需要知道如何读取文件路径,因为我必须从应用程序访问android上的特定文件。并且路径已经在代码中定义了,所以用户只会得到读数的输出。阅读文件路径android studio

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()); 
    } 

回答

1

不要使用硬编码的文件路径。该框架将为您提供要保存文件的区域的基本路径。

对于SD卡,使用Environment.getExternalStorageDirectory()

对于本地文件,使用Context.getFilesDir()(或Context.openFileOutput(String name, int mode)等)

对于本地缓存,使用Context.getCacheDir()

+0

所以基本上,我必须在把我的文件模拟器? –