创建于Android的子目录的文件子目录、文件、创建于、Android

2023-09-06 06:15:47 作者:花开、若相依

是否有可能创造下创建子目录中的文件/数据/数据​​/包名/文件/目录使用openFileOutput方法在android系统?即有一个子目录按名称文本里面的/数据/数据​​/包名/文件/目录。我想用openFileOutput(字符串名称,诠释模式)方法写一个文件,例如:SAMPLE.TXT到这个目录....是否有可能使用openFileOutput方法来做到这一点......

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....

推荐答案

不与openFileOutput,但你可以使用常规的java.io.File方法。

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

java.io.File.mkdir() 

要创建一个目录,例如复制从SD卡到一些数据子目录(可以调整创建)一个文件:

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);
    }
}
 
精彩推荐