2015-11-16 40 views
0

我正在嘗試使用Android Studio仿真在我的應用程序中生成json。有沒有辦法找到目錄並將其設置爲路徑並在下面實現它?如何在Android Studio仿真中找到寫入json文件的路徑?

...... 
    DIRECTORY directory = createDummySchool(); 
    ObjectMapper mapper = new ObjectMapper(); 

    try { 

    mapper.writeValue(new File(filepath? + "sample.json"), directory); 

    }catch (IOException e) { 
     e.printStackTrace();} 
    ..... 

回答

0

以及它取決於你想在哪裏寫入文件,內部或外部存儲器。

,你可以寫在內存的信息,它可以在你的應用程序訪問,但對外部存儲器寫你將需要獲得許可像

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

要保存文件內部,你可以使用這個

getFilesDir(); 
String filename = "myfile"; 
File file = new File(context.getFilesDir(), filename); 

String string = "Hello world!"; 

FileOutputStream outputStream;

try { 
outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
outputStream.write(string.getBytes()); 
outputStream.close(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

參照本作的詳細信息,這是最好的方式應該是你應該尋找放在首位,:)

+0

謝謝您的答覆。我的情況有點困難。我嘗試使用傑克遜來生成json,我想將它放入根目錄中的json文件中。您可以在以下鏈接中看到更多細節。 。謝謝...... – Spidey

+0

如果你願意,你可以保存你的文件名爲「anything.json」,並且你已經回答了你的問題,因爲你將會寫一個JSON字符串到文件中 –

相關問題