在android中的子目录中创建文件

use*_*193 2 android

是否可以使用android中的openFileOutput方法在"/ data/data/packagename/files /"目录下创建的子目录中创建文件?即在"/ data/data/packagename/files /"目录中有一个名为"text"的子目录.我想使用openFileOutput(String name,int mode)方法将文件例如:sample.txt写入此目录....是否可以使用openFileOutput方法来执行此操作....

谁可以帮我这个事....

Mat*_*adt 6

不是使用openFileOutput,而是可以使用常规的java.io.File方法.

java.io.File.mkdir() 
Run Code Online (Sandbox Code Playgroud)

创建目录,例如从sdcard复制(可以调整创建)文件到某个数据子目录:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)