Creating files in sub directories in android
Is it possible to create files in subdirectories created under "/data/data/packagename/files/" directory using openFileOutput method in android? ie there is a开发者_StackOverflow中文版 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....
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);
}
}
精彩评论