简体   繁体   English

如何在外部存储中的android中保存,读取和写入文件?

[英]How to save , read and write files in android in external storage?

I'm trying to understand how to work with files in Android 我正在尝试了解如何在Android中使用文件

I have installed my android application in external storage 我已经在外部存储中安装了Android应用程序

it contain a folder data of files that it need while running... 它包含运行时所需文件的文件夹data ...

firstly I think that my folder will saved automatically in the sdcard while installing 首先,我认为我的文件夹会在安装时自动保存在sdcard中

but after reading some question here , It seem I should create a folder in which I save my 但是在这里阅读了一些问题之后,看来我应该创建一个文件夹,在其中保存我的

folder data ... 资料夹data ...

I write this question to ask about steps to do to achieve my goal an I need your advances 我写这个问题来询问实现我的目标的步骤,我需要你的进步

Goal 目标

  • Save a folder data in external storage , read from it and write a new file 将文件夹data保存到外部存储中,从中读取并写入新文件

Steps 脚步

  • add permission to manifest file 向清单文件添加权限

  • create a folder in sdcard ==> how to place my folder there ? 在sdcard中创建一个文件夹==>如何将我的文件夹放在那里?

  • create a new file , save it on sdcard and write into it 创建一个新文件,将其保存在sdcard上并写入

code

This is a part of my activity : 这是我活动的一部分:

    {
      ....
       File file = new File ("...")

       doPrediction(file);


}

private void doPrediction (String FileName)

        {

     for (File child : VisualModels.listFiles()) {


     svmPredict.run(MyDataSource.inputPrediction,

         new File( MyDataSource.outputPrediction+ "/"+child.getName()+".predict"), 

         child);
          }
        }

I declared the class svmPredict as any ordinary java class ans this is the code of run method 我将类svmPredict声明为任何普通的Java类,这是run method的代码

       try 
    {
        BufferedReader input = new BufferedReader(new FileReader(inputFile));
        DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
        svm_model model = new svm().svm_load_model(modelFile);
        if(predict_probability == 1)
        {
            if(svm.svm_check_probability_model(model)==0)
            {
                System.err.print("Model does not support probabiliy estimates\n");
                System.exit(1);
            }
        }
        else
        {
            if(svm.svm_check_probability_model(model)!=0)
            {
                svm_predict.info("Model supports probability estimates, but disabled in prediction.\n");
            }
        }
        predict(input,output,model,predict_probability);
        input.close();
        output.close();
    } 

Please read the Android documentation regarding External Storage . 请阅读有关外部存储Android文档

Pay attention to the getExternalFilesDir() . 注意getExternalFilesDir() You can retrieve the path to the external storage using this method. 您可以使用此方法检索到外部存储的路径。 When you have the path, you can use the File object to create the data folder on the external storage like this: 有了路径后,可以使用File对象在外部存储上创建数据文件夹,如下所示:

File dataDirectory = new File(getExternalFilesDir() + "/data/");

Then create the folder by applying the mkdirs()-method on the File object: 然后通过在File对象上应用mkdirs()方法来创建文件夹:

dataDirectory.mkdirs();

To write to external storage, you will indeed have to declare this in the manifest: 要写入外部存储,您确实必须在清单中声明:

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

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

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