tags:

views:

74

answers:

1

Is it possible to create files in subdirectories created under "/data/data/packagename/files/" directory using openFileOutput method in android? ie there is a sub-directory by name "text" inside the "/data/data/packagename/files/" directory. I want to use the openFileOutput (String name, int mode) method to write a file eg:sample.txt into this directory.... Is it possible to use openFileOutput method to do it....

Can any one help me on this....

+1  A: 

Not with openFileOutput, but you can use the regular java.io.File methods.

java.io.File.mkdir() 

to create a directory, and for example for copying (can adjust for creating) a file from sdcard to some data subdir:

public static final void copyfile(String srFile, String dtFile){
    Log.d(MyApp.APP,"copyfile " + srFile + " -> " + dtFile ); 
    try{
        File f1 = new File(srFile);
        File f2 = new File(dtFile);
        InputStream in = new FileInputStream(f1);

        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        Log.d(MyApp.APP,"File copied to " + f2.getAbsolutePath());
    } catch(FileNotFoundException ex){
        Log.e(MyApp.APP,"Error.",ex);
    } catch(IOException e){
        Log.e(MyApp.APP,"Error.",e);
    }
}
Mathias Lin
Use `getFilesDir()` to get the base directory (what you have as `/data/data/packagename/files/` in your question).
CommonsWare