2016-10-13 39 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

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