简体   繁体   English

如何在Android Studio仿真中找到写入json文件的路径?

[英]How to find a path write to a json file in Android studio emulation?

I am trying use Android Studio emulation to produce a json in my apps. 我正在尝试使用Android Studio仿真在我的应用程序中生成json。 Is there a way to locate the directory and set it as path and implement it in the following? 有没有办法找到目录并将其设置为路径并在以下实现它?

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

    try {

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

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

well it depends where you want to write your file in, Internal or external memory. 取决于您要在内部或外部存储器中写入文件的位置。

you can always write information in internal memory which can be accessible by your app but to write on external memory you will require permission like 您总是可以在内部存储器中写信息,您的应用可以访问该信息,但是要在外部存储器上写信息,您需要获得以下权限

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

To save file internally you can use this 要在内部保存文件,您可以使用此

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

String string = "Hello world!";

FileOutputStream outputStream; FileOutputStream outputStream;

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

refer this for more information, this is the best way and should be the first place where you should be looking, :) 请参阅此以获得更多信息,这是最好的方法,应该是您应该查找的第一个地方,:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM