简体   繁体   中英

Create folder with contents in Android/data

I wanted to create a projects folder inside the Android/data folder, but it does not create one like com.projectname...

This is my actually code:

package com.project.p1;

import android.app.Activity;
import android.os.Bundle;

import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.createFiles();
    }

    public void createFiles() {
        try {
            File folder = new 
 File("Android/data/com.project.p1");
            File settings = new File(folder.getPath()+"/settings.yml");
            if(!(folder.exists())) {
                folder.mkdir();
            }
            if(!(settings.exists())) {
                settings.createNewFile();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

}

Never assume file paths in Android. The OEM is under no requirement to use a particular path. Use getFilesDir() to determine what the path is for files.

Also, that path wouldn't have been right anyway- its a relative path. You'd want an absolute. And I've never seen one that beings with Android.

public void createFiles() {
    try {
        File folder = getFilesDir();
        File settings = new File(folder.getPath()+"/settings.yml");
        if(!(folder.exists())) {
            folder.mkdir();
        }
        if(!(settings.exists())) {
            settings.createNewFile();
        }
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

That's my code now. But I don't know if something was created and where it was created...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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